在backbone-localStorage.js中参数化存储的名称
使用 Backbone.js 的 localStorage 插件的标准方法如下:
App.WordList = Backbone.Collection.extend({
initialize : function(models, options){
},
localStorage : new Store('English')
}
但我想用不同的语言制作不同的、并行的单词列表集合。因此,我希望能够在集合初始化时实例化 Store 的名称。 AFAICT,这工作正常:
App.WordList = Backbone.Collection.extend({
initialize : function(models, options){
this.localStorage = new Store(options.language);
}
}
然后我可以实例化一个 WordList,如下所示:
english = new Wordlist([], {language: 'English'});
或者:
chinese = new Wordlist([], {language: 'Chinese'});
问题是,我还没有在任何其他示例中真正看到这样做,我想知道是否有人会有任何“哎呀!不要这样做,因为……”这样的反应。
编辑
我应该补充一点,我已经尝试过这样做:
App.WordList = Backbone.Collection.extend({
initialize : function(models, options){
},
localStorage : new Store(options.store)
}
然后:
chinese = new Wordlist([], {language: 'Chinese'});
但由于某种原因 options.store
未定义。
The standard way to use the localStorage plugin for Backbone.js works like this:
App.WordList = Backbone.Collection.extend({
initialize : function(models, options){
},
localStorage : new Store('English')
}
But I want to make different, parallel wordlist collections in different languages. So, I want to be able to instantiate the name of the Store upon initialization of the collection. AFAICT, this works ok:
App.WordList = Backbone.Collection.extend({
initialize : function(models, options){
this.localStorage = new Store(options.language);
}
}
Then I can instantiate a WordList like:
english = new Wordlist([], {language: 'English'});
Or:
chinese = new Wordlist([], {language: 'Chinese'});
The thing is, I haven't really seen this done in any other examples and I'm wondering if anyone out there would have any "Eek! Don't do that, because..." sorts of reactions.
EDIT
I should add that I have already tried doing it this way:
App.WordList = Backbone.Collection.extend({
initialize : function(models, options){
},
localStorage : new Store(options.store)
}
And then:
chinese = new Wordlist([], {language: 'Chinese'});
But for some reason options.store
is coming up undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为答案更容易解释自己,所以我会继续给出一个答案。
在:
确实没什么不同
这与这样思考 ;传入 Backbone.Collection.extend(...) 的对象没有什么神奇之处。你只是传递一个普通的对象。当使用该对象作为参数调用 Backbone.Collection.extend 时,神奇的事情就会发生。
因此,对象方法 initialize 的 options 参数完全是完全正确的。与传入
new Store(...)
的内容不同。分配给initialize
的函数定义了选项的范围。谁知道new Store(options.store)
中引用的那个是在哪里定义的。它可以是window.options,也可以是在其他范围内定义的选项。如果未定义,您可能会收到错误话虽如此,我只看到两三个战略选项(哦天哪,请原谅双关语!)。
每当您创建集合的新实例时,可以:
new Store(..)
。您需要弄清楚的是这些策略中哪一种应该适合您。我从未使用过 localStorage,因此不幸的是,我无法在这方面为您提供帮助。我能做的是问,是否会从 App.Wordlist 创建多个实例,其中可能会意外创建两个相同类型的 Store?
事实上,我还有一个问题。这个Store
在哪里定义的?您确定您正在使用的其他 API 库中没有定义它吗?仔细阅读我所知道的 localStorage 文档,提到了一些Storage
构造函数 但没有什么商店
。所以你可能也想弄清楚这一点。编辑 #1:没关系,我看到你提到了
Store
已定义。It's easier to explain myself as an answer, so I'll go ahead and give one.
In:
This is really little different from
Think of it this way; there's nothing magical about the object being passed in to
Backbone.Collection.extend(...)
. You're just passing in an ordinary object. The magic happens whenBackbone.Collection.extend
is invoked with that object as a parameterThus, the options parameter of the object method
initialize
is completely different that which is being passed in tonew Store(...)
. The function being assignedinitialize
is defining the scope of options. Who knows where the one referred to innew Store(options.store)
is defined. It could bewindow.options
or it could be options defined in some other scope. If it's undefined, you're likely getting an errorThat being said, I only see two or three strategic options (oh jeez, forgive the pun please!).
Whenever you're creating a new instance of the collection, either:
new Store(..)
where needed.What you need to figure out is which one of those strategies should work for you. I have never used localStorage so, unfortunately, I can't help you in that regard. What I can do is ask, is there ever going to be multiple instances created from App.Wordlist where there might accidentally be created two of the same kind of Store?
In fact, I've got another question. where is thisStore
defined? Are you sure that's not defined somewhere in one of your other API libraries you're using? Perusing the localStorage docs I know about mentions something of aStorage
constructor but nothing of aStore
. So you might want to figure out that as well.Edit #1: Nevermind, I see you mentioned where
Store
was defined.我通过创建一个方法来解决这个问题,该方法允许您在实例化后配置 localStorage:
然后您可以在设置集合后设置 localStorage:
I got around this by creating a method which allows you to configure the localStorage after instantiation:
you can then set the localStorage after you have set up the collection: