简体   繁体   中英

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

I have a simple Ember.js app. There's an application view and inside that view there's a results view .

I'm using ember-model to fetch JSON from the server that way:

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(); (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.

application.hbs

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

application_controller.js

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

results.hbs

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

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).

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 :

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)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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