admin管理员组文章数量:1318572
I use Backbone.js to create a web app,all the view,collection and model write into one js file,it success!
now I want separate them to different js files,just like:
<script type="text/javascript" src="js/layermanagemodel.js"></script>
<script type="text/javascript" src="js/layermanagecollection.js"></script>
<script type="text/javascript" src="js/layermanageview.js"></script>
<script type="text/javascript" src="js/boot.js"></script>
and load model code in jquery load:
$(function(){
//Model
var manageModel = Backbone.Model.extend({
default:{
'selectedId':'unknow'
},
selectLayer:function(uuid){
this.set({"selectedId": uuid});
},
delLayer:function(){
}
});
})
but the firebug tell me bug:
manageModel is not defined
[Break On This Error]
model: manageModel
in collection file
why if separate them to different file ,they could not recognize each other?how can I solve this problem?Or what is right load order?thank you
I use Backbone.js to create a web app,all the view,collection and model write into one js file,it success!
now I want separate them to different js files,just like:
<script type="text/javascript" src="js/layermanagemodel.js"></script>
<script type="text/javascript" src="js/layermanagecollection.js"></script>
<script type="text/javascript" src="js/layermanageview.js"></script>
<script type="text/javascript" src="js/boot.js"></script>
and load model code in jquery load:
$(function(){
//Model
var manageModel = Backbone.Model.extend({
default:{
'selectedId':'unknow'
},
selectLayer:function(uuid){
this.set({"selectedId": uuid});
},
delLayer:function(){
}
});
})
but the firebug tell me bug:
manageModel is not defined
[Break On This Error]
model: manageModel
in collection file
why if separate them to different file ,they could not recognize each other?how can I solve this problem?Or what is right load order?thank you
Share Improve this question asked May 18, 2012 at 3:46 hh54188hh54188 15.6k35 gold badges116 silver badges191 bronze badges2 Answers
Reset to default 9Once you add the function wrappers:
$(function() {
// ...
})
You've introduced new scopes and all the var
s declared inside those functions are only visible within those functions. You can get around this by making them global (i.e. properties of window
):
$(function(){
window.manageModel = Backbone.Model.extend({
//...
});
});
or better, introduce an application namespace:
$(function(){
window.app = window.app || { };
window.app.manageModel = Backbone.Model.extend({
//...
});
});
and then refer to things through app
like app.manageModel
:
$(function(){
window.app = window.app || { };
window.app.someCollection = Backbone.Collection.extend({
model: app.manageModel,
//...
});
});
You could also look at making you js files modular using Require.js. Works extremely well and will only load the views, models, and collections when they are needed. This is remended if your application is quite large. It will prevent you from having to load all your scripts on page load. A quick backbone.js implementation would be as follows:
define([
'jquery',
'underscore',
'backbone',
'models/post'
], function ($, _, Backbone, Post) {
"use strict";
var PostsCollection = Backbone.Collection.extend({
model: Post,
url: CONFIG.apiUrl + 'posts'
});
return PostsCollection;
});
The above is a collection module. You can see 'models/post' is pointing to the location of another module. jquery, underscore, and backbone were defined in my config so I just have to pass them in as opposed to pointing to their actual location. This is a quick intro, but if you are looking to separate your js files, Require.js is the way.
本文标签:
版权声明:本文标题:javascript - Backbone.js:separate the view ,collection,model to different js file,they could't recognize each other - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741079698a2335933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论