max*_*022 9 extends backbone.js backbone-views
我需要创建一个基本视图,我的所有视图都将扩展.我不确定在何时何地宣布这个观点.
基本上,我需要注入global variables我的所有模板,而不是在每个render()方法中都这样做.
这是我现在的树形结构:
|-main.js
|-app.js
|-require.js
|-App
| |-View
| | |-Dashboard.js
| | |-Header.js
| | |-Content.js
| |-Model
| |-Collection
| |-Template
|
|-Libs
|-...
Run Code Online (Sandbox Code Playgroud)
这是我的app.js
var App = {
ApiURL: "http://domain.local",
View: {},
Model: {},
Collection: {},
Registry: {},
Router: null
};
define(['backbone', 'View/Dashboard'], function(Backbone){
var AppRouter = Backbone.Router.extend({
routes : {
"dashboard": "index",
},
index: function() {
console.log('routing index route...');
var x = new App.View.Dashboard({el:$('#main-content'), type:'other'});
}
});
var initialize = function() {
App.Router = new AppRouter;
Backbone.history.start({pushState: true});
console.log('Backbone is running...');
};
return {
initialize : initialize
};
});
Run Code Online (Sandbox Code Playgroud)
现在,我的所有观点都继承自Backbone.View:
App.View.Dashboard = Backbone.View.extend({
Run Code Online (Sandbox Code Playgroud)
我想创建自己Base View的应用程序的所有视图将扩展.这是我到目前为止所做的,但我不知道在哪里放置这段代码,因为在app.js中我正在加载Dashboard视图所以我需要先做它但我需要这个基本视图在所有视图中的对象...所以我迷路了:(
define(['backbone', 'underscore', 'twig'], function(Backbone, _){
App.View.Base = Backbone.View.extend({});
_.extends(App.View.Base.prototype, {
initialize: function(params)
{
this.el = params.el;
this.init(params);
},
init: function(params)
{
},
renderTemplate:function(template_path, data)
{
var tpl = twig({href:template_path, async:false});
// Inject variables
data.user = App.Registry.User;
data.account = App.Registry.Account;
return tpl.render(data);
}
});
});
Run Code Online (Sandbox Code Playgroud)
欢迎任何想法或评论.答案是最好的:D
谢谢,Maxime.
fgu*_*len 11
App.View.Base = Backbone.View.extend({
baseMethod: function( params ) {};
});
App.ExtendedView.Base = App.View.Base.extend({
// new stuff here
// overriding App.View.Base.baseMethod
baseMethod: function( params ) {
// overriding stuff here
App.View.Base.prototype.baseMethod.call(this, params); // calling super.baseMethod()
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5291 次 |
| 最近记录: |