backbone.js - RequireJS/Backbone : inheritance of dependencies? -
update : i've found solution slighty same in first answer. but in fact i'd know if there manner using requirejs, without using special var or parameters in views. here solution :
define(['resthub', 'backbone', 'views/mymodule/parent-view', 'views/mymodule/a-view'], function(resthub, backbone, parentview, aview) { var parentview = backbone.view.extend({ // stuff before buildaview : function(aviewobject){ var aview = aviewobject || aview; // enought source code before , after following lines // don't want duplicate in childview this.aview = new aview(); this.aview.render(); } // stuff after }); return parentview; });
i try use maximum inheritance in backbone project dependencies managed requirejs avoid duplicate code. in fact, create new view extend base view. base view has dependency want override. if try override, original dependency taken, instead of new one. note i'm forced inherit.
here i'm trying :
base view inherit :
define(['resthub', 'backbone', 'views/mymodule/parent-view', 'views/mymodule/a-view'], function(resthub, backbone, parentview, aview) { var parentview = backbone.view.extend({ // stuff before buildaview : function(){ // enought source code before , after following lines // don't want duplicate in childview this.aview = new aview(); this.aview.render(); } // stuff after }); return parentview; });
then view attempt create. want buildaview() function takes new dependency called aview in b-view contains not same source code in a-view.
define(['resthub', 'backbone', 'views/mymodule/parent-view', 'views/mymodule/b-view'], function(resthub, backbone, parentview, aview) { var childview = parentview.extend({ // stuff before render: function() { parentview.__super__.render.apply(this, []); /* stuff inbetween*/ this.buildaview(); } }); return childview; });
thanks :)
the problem aview
bound function injected, when call buildaview
use aview
injected parentview
module. there simple way fix problem. save aview
property of view.
define(['resthub', 'backbone', 'views/mymodule/parent-view', 'views/mymodule/a-view'], function(resthub, backbone, parentview, aview) { var parentview = backbone.view.extend({ // stuff before aview: aview, buildaview : function(){ // enought source code before , after following lines // don't want duplicate in childview this.aview = new this.aview(); this.aview.render(); } // stuff after }); return parentview; }); define(['resthub', 'backbone', 'views/mymodule/parent-view', 'views/mymodule/b-view'], function(resthub, backbone, parentview, aview) { var childview = parentview.extend({ // stuff before aview: aview, render: function() { parentview.__super__.render.apply(this, []); /* stuff inbetween*/ this.buildaview(); } }); return childview; });
Comments
Post a Comment