我有config.json
我将作为Backbone模型加载到我的应用中,如:
var Config = Backbone.Model.extend({
defaults: {
base: ''
},
url: 'config.json'
});
其他模型应该依赖于Config
中包含的某些数据,如:
var ModelA = Backbone.Collection.extend({
initialize: function(){
//this.url should be set to Config.base + '/someEndpoint';
}
});
在上面的示例中,ModelA
的{{1}}属性取决于url
的{{1}}属性的值。
如何在Backbone应用程序中正确设置?
答案 0 :(得分:2)
在我看来,你的基本问题是:
url
?url
函数?有很多方法可以解决这个问题,但我会建议一些细节,以便我可以提供指导和代码,并且#34;完成它,"可以这么说。
我认为处理第一个问题的最佳方法是使该配置模型成为单例。我将在下面的backbone-singleton GitHub页面中提供代码,但我不希望答案垂直延长,直到我完成了解释,所以请继续阅读......
var MakeBackboneSingleton = function (BackboneClass, options) { ... }
接下来,我们利用AppConfiguration
创建单身deferred
以及jQuery
属性。 fetch
的结果将提供always(callback)
,done(callback)
等
var AppConfiguration = MakeBackboneSingleton(Backbone.Model.extend({
defaults: {
base: null
},
initialize: function() {
this.deferred = this.fetch();
},
url: function() {
return 'config.json'
}
}));
现在,是时候定义看起来像你的依赖模型DependentModel
了。它会调用AppConfiguration()
来获取实例。
请注意,由于MakeBackboneSingleton
,所以true
:
var instance1 = AppConfiguration();
var instance2 = new AppConfiguration();
instance1 === instance2; // true
instance1 === AppConfiguration() // true
在提供fetch
时,模型会自动id
,但之后只有我们已完成AppConfiguration
的提取。请注意,您可以使用always
,then
,done
等
var DependentModel = Backbone.Model.extend({
initialize: function() {
AppConfiguration().deferred.then(function() {
if (this.id)
this.fetch();
});
},
url: function() {
return AppConfiguration().get('base') + '/someEndpoint';
}
});
现在最后,将它们放在一起,你可以实例化一些模型。
var newModel = new DependentModel(); // no id => no fetch
var existingModel = new DependentModel({id: 15}); // id => fetch AFTER we have an AppConfiguration
只要AppConfiguration
的抓取成功,第二个就会自动抓取。
此处为您MakeBackboneSingleton
(再次来自GitHub存储库):
var MakeBackboneSingleton = function (BackboneClass, options) {
options || (options = {});
// Helper to check for arguments. Throws an error if passed in.
var checkArguments = function (args) {
if (args.length) {
throw new Error('cannot pass arguments into an already instantiated singleton');
}
};
// Wrapper around the class. Allows us to call new without generating an error.
var WrappedClass = function() {
if (!BackboneClass.instance) {
// Proxy class that allows us to pass through all arguments on singleton instantiation.
var F = function (args) {
return BackboneClass.apply(this, args);
};
// Extend the given Backbone class with a function that sets the instance for future use.
BackboneClass = BackboneClass.extend({
__setInstance: function () {
BackboneClass.instance = this;
}
});
// Connect the proxy class to its counterpart class.
F.prototype = BackboneClass.prototype;
// Instantiate the proxy, passing through any arguments, then store the instance.
(new F(arguments.length ? arguments : options.arguments)).__setInstance();
}
else {
// Make sure we're not trying to instantiate it with arguments again.
checkArguments(arguments);
}
return BackboneClass.instance;
};
// Immediately instantiate the class.
if (options.instantiate) {
var instance = WrappedClass.apply(WrappedClass, options.arguments);
// Return the instantiated class wrapped in a function so we can call it with new without generating an error.
return function () {
checkArguments(arguments);
return instance;
};
}
else {
return WrappedClass;
}
};