简体   繁体   English

每当有Ember-Model的新记录时更新视图

[英]Updating a view whenever there's new records with Ember-Model

I have a simple Ember.js app. 我有一个简单的Ember.js应用程序。 There's an application view and inside that view there's a results view . 有一个application view ,在该视图内有一个results view

I'm using ember-model to fetch JSON from the server that way: 我正在使用ember-model以这种方式从服务器获取JSON:

Map.Winery = Ember.Model.extend({
    name:          Ember.attr(),
    address:       Ember.attr(),
    address_2:     Ember.attr(),
    city:          Ember.attr(),
    stateprovince: Ember.attr(),
    country:       Ember.attr(),
    latitude:      Ember.attr(),
    longitude:     Ember.attr(),

    full_address: function () {
        return this.get('address') + ' ' + this.get('city') + ' ' + this.get('stateprovince') + ' ' + this.get('country');
    }.observes('address', 'city', 'stateprovince', 'country')
});

Map.Winery.adapter = Ember.Adapter.create({
    findAll: function(klass, records) {
        return Ember.$.getJSON('/api/wineries').then(function(response) {
            if (response.success) {
                records.load(klass, response.data);
            }
        });
    }
});

I made it as simple as possible so people can have a fair look at what's going on. 我使它尽可能的简单,以便人们可以对发生的事情有个公正的了解。

So basically, I have a button in my application view that whenever I click, calls an action that calls Map.Winery.findAll(); 因此,基本上,我在应用程序视图中有一个按钮,每当我单击该按钮时,都会调用一个调用Map.Winery.findAll()的操作; (reloads data from the server). (从服务器重新加载数据)。

What I'd like is, whenever there's new data loaded in the records, the results view should update with the new results. 我想要的是,每当记录中加载了新数据时, results view应使用新结果进行更新。

application.hbs application.hbs

<button class="locate" {{action "reload" on="click"}}>Reload</button>
{{view Map.ResultsView}}

application_controller.js application_controller.js

Map.ApplicationController = Ember.ObjectController.extend({
    actions: {
        reload: function() {
            var results = Map.Winery.findAll();
        }
    }
});

results.hbs results.hbs

<div class="results">
    {{#each results}}
        <p>{{this}}</p>
    {{/each}}
</div>

results_view.js results_view.js

Map.ResultsView = Ember.View.extend({
    templateName: 'results',

    didInsertElement: function(e) {
    }

});

Now question is: How do I make it so whenever there's new data loaded from the server to the records, results view gets updated? 现在的问题是:如何做到这一点,以便每当有新数据从服务器加载到记录时,结果视图都会更新?

I tried inserting as much related code as possible, feel free to ask any questions. 我尝试插入尽可能多的相关代码,请随时提出任何问题。

Thanks 谢谢

Just push the new objects into your collection (or replace the collection with the new collection from findAll). 只需将新对象推入您的集合中即可(或使用findAll中的新集合替换该集合)。

http://emberjs.jsbin.com/EFiTOtu/1/edit http://emberjs.jsbin.com/EFiTOtu/1/edit

App.IndexController = Em.ArrayController.extend({
  actions: {
    addNewColor: function(){
      this.get('model').pushObject(this.get('newColor'));
    }
  }
});

Best way I found is to add an event to whatever I need to be triggered and use that trigger whenever I need it. 我发现最好的方法是将事件添加到需要触发的任何事件中,并在需要时使用该触发器。

Using Ember.Evented : 使用Ember.Evented

App.ApplicationController = Ember.ObjectController.extend(Ember.Evented, {
    viewUpdated: function(args) {} 
    /* controller stuff here */ 
});

I'm now able to add events like this from almost anywhere: 现在,我几乎可以在任何地方添加这样的事件:

this.get('controller').on('viewUpdated', $.proxy(this.viewUpdated, this));

So whenever I have to trigger the view to update I do: 因此,每当我必须触发视图更新时,我都会执行以下操作:

this.get('controllers.application').trigger('viewUpdated', args);

(args is optional by the way) (顺便说一句,args是可选的)

That way the event gets triggered whenever I want to. 这样,无论何时我想触发事件。 It's not as powerful as an observer but still it does the job pretty well! 它不像观察员那么强大,但仍然可以很好地完成工作!

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

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