ember.js - What is the proper way to subscribe to model events in a view? -
i want subscribe model's becameinvalid , becameerror events in view can set error state. can think of 2 ways, both have drawbacks.
the first way can think of utilize observers create own becameinvalid , becameerror hooks on view:
becameinvalid: function() { var isvalid = this.get('controller.model.isvalid'); if (ember.isnone(isvalid) || isvalid) { return; } console.log('becameinvalid'); }.observes('controller.model.isvalid') becameerror: function() { var iserror = this.get('controller.model.iserror'); if (ember.isnone(iserror) || !iserror) { return; } console.log('becameerror'); }.observes('controller.model.iserror')
this works, uses lot of boilerplate code. sec way can think of add together handlers events after model loaded:
didload: function() { var isloaded = this.get('controller.model.isloaded'); if (ember.isnone(isloaded) || !isloaded) { return; } var model = this.get('controller.model'); model.on('becameinvalid', function() { console.log('becameinvalid'); }); model.on('becameerror', function() { console.log('becameerror'); }); }.observes('controller.model.isloaded')
the problem approach still requires boilerplate code , (correct me if i'm wrong) event handlers won't cleaned automatically when view removed.
what i'd similar observers protocol events cleaned automatically framework. e.g.:
becameinvalid: function() { console.log('becameinvalid'); }.eventhandler('controller.model.becameinvalid') becameerror: function() { console.log('becameerror'); }.eventhandler('controller.model.becameerror')
does exist?
i think first proposal feasible solution. here version of it, little bit less verbose:
isvalid : ember.computed.alias("controller.model.isvalid"), becameinvalid: function() { if(this.get("isvalid"){ return; } console.log('becameinvalid'); }.observes('isvalid')
see question details on ember.computed.alias. apart that, have found no possibilities of triggering observers under circumstances (= when isvalid turns false) out-of-the-box functionality ember. have implement such observer on own.
ember.js
No comments:
Post a Comment