简体   繁体   English

将Ember从Ember.Object虚拟数据迁移到$ .get数据时出现问题

[英]Problems migrating Ember from Ember.Object dummy data to data from $.get

I simply want a single $.get call to satisfy a particular model. 我只希望一个$ .get调用来满足特定模型。 I can't seem to find any documentation that clearly shows how to go about doing this? 我似乎找不到任何能清楚说明如何执行此操作的文档?

ie I have an application controller as: 即我有一个应用程序控制器为:

App.ApplicationController = Ember.ObjectController.extend({
  username: '',
  firstname: '',
  lastname: ''
});

How do I set the username with data from a $.get? 如何使用$ .get中的数据设置用户名? I know there is Ember Data and other libraries that do this, but I'd like to do this without those libraries and furthermore better understand how Ember works with Asynchronous data.. Their guides unfortunately didn't help much :/ 我知道有Ember Data和其他库可以执行此操作,但是我想在没有这些库的情况下执行此操作,并且更好地了解Ember如何处理异步数据。.不幸的是,他们的指南没有太大帮助:/

Thanks for any help :) 谢谢你的帮助 :)

UPDATE UPDATE

Thanks guys for the answers so far, but I don't think I explained my problem sufficiently. 到目前为止,谢谢大家的回答,但是我认为我对问题的解释不够充分。

I have application.hbs : 我有application.hbs

<div class="medium-8 columns">
 <dl class="account-menu right">
   <dd><p><a href="">Welcome, {{username}}</a></p></dd>
   <dd><p><a href="">Customer Support</a></p></dd>
   <dd><a href="#">Log Out</a></dd>
  </dl><!-- .acccount-menu -->
</div>

I have Model: 我有模特:

App.User = Ember.Object.extend({
  id      : '', 
  firstName : '',
  lastName  : '',
  username  : '',

  init: function(){
    console.log(App.Account.get('username');
  },

  fullName: function(){
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName'), 

}).reopenClass({
  loadAccount: function(){
    return $.get( "http://localhost:8080", { id: 1, request: "account" }).then(function(response){ 
      App.Account = App.User.create({'username': response.username});
    });
  }
});

The Controller is the same as above. 控制器与上述相同。 I am successfully Creating a new App.Account from App.User.create and successfully triggering the App.User abstract class? 我已成功从App.User.create创建新的App.Account并成功触发了App.User抽象类? init method.. Problem is {{username}} never gets updated? init方法。问题是{{username}}从未更新? I know I'm missing something simple here! 我知道我在这里缺少一些简单的东西! Thanks again for all your help 再次感谢你的帮助

You would set the model of your ApplicationRoute to the JSON you fetch: 您可以将ApplicationRoute的模型设置为您获取的JSON:

App.ApplicationRoute = Ember.Route.extend({
   model: function() {
     return $.getJSON('your_url_goes_here');
   }
})

This way every time the route is entered the model (and thus implicitly the content of your controller) will be set to the JSON returned by your AJAX call. 这样,每次输入路线时,模型(以及因此隐含的控制器content )将被设置为AJAX调用返回的JSON。

The model is set with the model hook of the associated route. 使用相关路线的model挂钩设置model In your case: 在您的情况下:

App.ApplicationRoute = Ember.Route.extend({
    model: function(){
        return $.get.......
    }
});

The model hook will accept an asynchronous call, but ember will not continue to render the view until the model hook promise is complete. 模型挂钩将接受异步调用,但是在模型挂钩承诺完成之前,ember不会继续渲染视图。

Guys thanks so much for your help.. Turns out it was a simple oversight... 伙计们非常感谢您的帮助。事实证明这是一个简单的疏忽。

I was not returning the Object Model promise 'App.User' that I had created in the loadAccount method, which is called by the ApplicationRouter model hook. 没有返回我在loadAccount方法中创建的对象模型承诺'App.User',该方法由ApplicationRouter模型挂钩调用。

Bad Code 错误代码

loadAccount: function(){
  return $.get( "http://localhost:8080", { id: 1, request: "account"    }).then(function(response){ 
    App.Account = App.User.create({'username': response.username});
});

Good Code 好代码

loadAccount: function(){
  return $.get( "http://localhost:8080", { id: 1, request: "account"    }).then(function(response){ 
    return App.User.create({'username': response.username});
});

It makes sense. 这说得通。 The $.get method returns a promise to my model that I never delivered on with a return value inside the .then() method $ .get方法向我的模型返回一个从未兑现的承诺,并在.then()方法内返回一个值

Thank you all for you answers and attention to the matter 谢谢大家对这个问题的回答和关注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM