gpt4 book ai didi

javascript - 了解 BackboneJS

转载 作者:行者123 更新时间:2023-12-02 19:14:19 27 4
gpt4 key购买 nike

我刚刚开始学习 BackboneJS,我正在编写一个小示例文档来测试它的工作方式。但我很难理解它,目前它根本没有任何意义:)

所以,我有下一个文件结构:

/* application.js */
var Application = Application || {};

;(function ( $, window, document, undefined ) {

new Application.ApplicationView();

})( jQuery, window, document );


/* sample.js */
var Application = Application || {};

;(function ( $, window, document, undefined ) {

Application.ApplicationView = Backbone.View.extend({

});

})( jQuery, window, document );


/* utilities.js */
var Application = Application || {};

;(function ( $, window, document, undefined ) {

Application.ApplicationUtilities = Backbone.Collection.extend({

polyfillize : function(tests) {

return _.each(tests, function(obj){
console.log(obj);
Modernizr.load([{
test : _.reduce(obj.test, function(initial_value, current_value){
return initial_value && current_value;
}, 1),
nope : obj.polyfill,
callback: function(url, result, key) {
console.log('Polyfill : ', [ url ]);
}
}]);
});
}

});

})( jQuery, window, document );


/* options.js */
var Application = Application || {};

;(function ( $, window, document, undefined ) {

Application.ApplicationOptions = Backbone.Model.extend({

version : [ 1 ],

prettify : true,

libraries : {

google_code_prettyfier : 'assets/js/libraries/google/prettify.js'

},

dependencies : {

google_code_prettyfier : function() {
return Modernizr.load([{
test : this.prettify,
yep : [ this.libraries.google_code_prettyfier ],
callback: function(url, result, key) {
return window.prettyPrint && prettyPrint();
}
}]);
}
},

polyfills : {
json_polyfill_url : 'http://cdn.chaoscod3r.com/code/polyfills/json3_polyfill.js',
storage_polyfill_url : 'http://cdn.chaoscod3r.com/code/polyfills/localstorage_polyfill.js'
}

});

})( jQuery, window, document );


/* polyfills.js */
var Application = Application || {};

;(function ( $, window, document, undefined ) {

Application.ApplicationPolyfills = Backbone.Model.extend({

loadPolyfills : function() {
return Application.ApplicationUtilities.polyfillize([
{
test : [Modernizr.localstorage, Modernizr.sessionstorage],
polyfill : [Application.ApplicationOptions.polyfills.storage_polyfill_url]
},
{
test : [Modernizr.json],
polyfill : [Application.ApplicationOptions.polyfills.json_polyfill_url]
}
]);
}

});

})( jQuery, window, document );

每个模型都放置在其自己的模型文件夹中,每个集合都放置在其集合文件夹中,依此类推。然后我加载 DOM 中的所有脚本,首先是集合,然后是模型、 View ,最后是 application.js

我想要做的是能够将集合用作函数集合,我可以在 View 或模型中随时使用这些函数,这些选项应该在任何级别和任何集合/模型中可用/意见。因此,我的 sample.js 文件将包含主视图,它将初始化需要在 DOM 加载上运行的所有内容,例如在必要时加载所有填充,启用 google 代码美化器等等。

显然在这种状态下什么都不起作用,所以如果可能的话,我希望有更有经验的人来帮助我,或者指出一些与我现在正在尝试做的事情相匹配的教程:)

最佳答案

您似乎对 Backbone 集合的含义有点困惑。

一个collection in Backbone是(Backbone)模型的集合:

Backbone.Collection

Collections are ordered sets of models. [...]

当你这样说时:

var C = Backbone.Collection.extend({
method: function() { ... }
});

您正在创建一个“类”,并且在创建新实例后,方法将可用作“实例方法”,但您无法直接调用方法C 上,因为 extendmethod 放在 C.prototype 上进行继承:

C.method(); // This doesn't work, you'll just get an error.
var c = new C;
c.method(); // This works

你说的是

it made sense to have it in a collection, a collection of utility functions :)

但是,不幸的是,仅仅因为某些事情有意义并不意味着它真的是明智的:)

您没有任何 Application.ApplicationUtilities 模型,因此它不是 Backbone 意义上的集合。我认为你只是想使用一个对象作为命名空间:

Application.ApplicationUtilities = {
polyfillize: function(tests) { /* ... */ }
};

关于javascript - 了解 BackboneJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com