也许Backbone.js(或其他frameworks之一)可能是您正在寻找的救援的一部分。
我发现骨干非常有帮助组织一些遗传意大利面条。你的出发点可能是转换您的海量文档准备到骨干视图(或倍数)
通过分离出来的意见,集合,型号为单独的文件,然后捆绑并一起再压缩成一个单一的文件组织您的脚本,以便浏览器只需要提出一个请求而不是很多。
ASP.NET MVC4可以做bundling对你来说,这也同样适用于MVC3
这仅仅是简单的起点的一个例子,有更先进的技术(如AMD,require.js)至减少每页的脚本大小,但是使用缓存和gzip我发现单一的一切脚本包在很多情况下都很好。
至于你的例子,这里有一个可能的骨干实现
记住命名空间你的代码...
var app = app || {};
$(function ($) {
// depending on your server setup you might be able to just override the url
// and get away with what you want. Otherwise you might look into overriding
// the save/fetch or sync methods of the collection
app.MyListCollection = Backbone.Collection.extend({
url: '/lists/getmylist'
});
app.MyListView = Backbone.View.extend({
//bind the view to the existing element in the HTML.
el: '#MyList',
// your mustache template
template:$('#list-template').html(),
// hook up your event handlers to page components
//(within the context of your el)
events: {
'click #MyButton': 'onMyButtonClick'
},
//called on object creation.
initialize: function() {
//you could create a collection here or pass it into the init function
this.collection = new app.MyListCollection();
//when the collection is changes, call the render method
this.listenTo(this.collection, 'reset', this.render);
},
// render is named by convention, but its where you would "redraw" your view and apply your template
render: function() {
this.$el.html(
Mustache.render(
this.template(this.collection.toJSON())));
return this;
},
//your click handler
onMyButtonClick: function(e){
this.collection.fetch();
}
});
});
使用您的文档准备旋转起来不管骨干功能,你需要 ,并用它用您可能拥有的任何服务器端数据引导您的JavaScript。
$(function() {
// create an instance of your view
new app.MyListView();
//example bootstrap using razor
app.title = @Model.Title;
});
如果您愿意,我可以将其迁移到我们的Code Review测试版网站。你可能会在那里得到更好的答案。 @如果你愿意的话,回复我。 – Kev 2013-03-16 17:39:46