mercredi 31 décembre 2014

How do I create custom events in Ember.js, or organize asynchronous behavior?

In my controller I update some visual indexes when rows are added/removed from my two tables. For logging purposes, I need to post to the server what those changes are. Those changes happen after the data changes thanks to an observer, but I want to then notify the server of the changes after that. See below for my 'afterIndexesAreUpdated' pseudo event. If I change that to the same observer that resetIndexes listens to, it will be called before the data is actually updated. I need it to trigger after resetIndexes finishes, but I can't figure out how to do that. What do I need to do? I can't find any examples of dealing with events other than 'afterRender'.



resetIndexes: function() {
var index = 1;
this.get('listA').forEach(function(story) {
story.set('index', index);
index += 1;
});
this.get('listB').forEach(function(story) {
story.set('index', index);
index += 1;
});

// TODO: trigger event: afterIndexesAreUpdated
},

indexesObserver: function() {
Ember.run.once(this, this.resetIndexes);
}.observes("listA.@each", "listB.@each").on('afterRender'),

addToListB: function(story) {
var attrs = $.extend({}, story._attributes, story.get('_data'));
var oldIndex = story.get('index');

this.store.createRecord('story-b', attrs);
story.deleteRecord();

Ember.run.scheduleOnce('afterIndexesAreUpdated', function() {
var newIndex = story.get('index');
$.post("/move", { story_id: story.get('id'), position: newIndex, old_position: oldIndex }, function(data) {

}).fail(function() {
var storyName = story.get('title');
alert( "Something went wrong moving " + storyName );
});
});

},




Aucun commentaire:

Enregistrer un commentaire