ember.js - Ember setupController fires observes, how can i prevent that -
my code looks this
app.itemroute = em.route.extend({ setupcontroller: function(controller) { var model = this.modelfor('item'); controller.setproperties({ name : model.get('name'), title: model.get('title') }); } }); app.itemcontroller = em.objectcontroller.extend({ saveonchange: function() { console.log('saveonchange'); }.observes('name', 'title'), }); from understanding because using setproperties observe should fire 1 time , fire 2 times
also wrapping setproperties beginpropertychanges & endpropertychanges still fires twice
what not fire @ all, ended doing changing controller code this
app.itemcontroller = em.objectcontroller.extend({ load: false, saveonchange: function() { if(!this.get('load')) { this.set('load', true); return; } console.log('saveonchange'); }.observes('name', 'title'), }); this code work if alter fired once, won't work if fired multiple times (that's case)
the setproperties function doesn't coalesce observers (unfortunately there's no way that), groups them 1 operation. source might help improve see does:
ember.setproperties = function(self, hash) { changeproperties(function() { for(var prop in hash) { if (hash.hasownproperty(prop)) { set(self, prop, hash[prop]); } } }); homecoming self; }; so, problem. best way can think of debounce function.
app.itemcontroller = em.objeccontroller.extend({ load: false, saveonchange: function() { em.run(this, 'debouncedsave', 150); }.observes('name', 'title'), debouncedsave: function() { if(!this.get('load')) { this.set('load', true); } } }); if you're not familiar debouncing, can read here. there other solutions involving direct manipulation of properties, i'm not sure if that's road want go down.
ember.js
No comments:
Post a Comment