在我的页面顶部,我获取了一个隐藏输入字段的值,将其存储为一个变量,然后尝试在我的 Backbone 代码中使用它。但是,该值显示为未定义。如何先获取字段的值,然后初始化我的主干应用程序?
<script>
$(document).ready(function(){
var whitedealsToken = $('#usertoken').val();
console.log(whitedealsToken)
WhiteDeals.initialize();
});
</script>
编辑:下面的完整骨干代码
window.WhiteDeals = {
Models: {},
Collections: {},
Views: {},
Routers: {},
initialize: function() {
new WhiteDeals.Routers.Deals();
if (!Backbone.history.started) {
Backbone.history.start();
Backbone.history.started = true;
}
}
};
WhiteDeals.Routers.Deals = Backbone.Router.extend({
routes: {
"": "index"
},
index: function() {
var deals = new WhiteDeals.Collections.Deals();
var dealsview = new WhiteDeals.Views.DealsIndex({
el: $('#container')
});
}
});
WhiteDeals.Collections.Deals = Backbone.Collection.extend({
model: WhiteDeals.Models.Deal,
url: 'http://lvh.me:3000/api/v1/special_deals?access_token=' + whitedealsToken,
parse: function(response) {
return response.results;
},
// Overwrite the sync method to pass over the Same Origin Policy
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: false
}, options);
return $.ajax(params);
}
}); // End Collection