在backbone-localStorage.js中参数化存储的名称

发布于 2024-12-25 14:43:54 字数 1190 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜`诱少女 2025-01-01 14:43:54

作为答案更容易解释自己,所以我会继续给出一个答案。

在:

App.WordList = Backbone.Collection.extend({
  initialize : function(models, options){
      ....   
  },
  localStorage : new Store(options.store)
})

确实没什么不同

var newInstanceConfig = {
  initialize : function(models, options){
      ....   
  },
  localStorage : new Store(options.store)
}
App.WordList = Backbone.Collection.extend(newInstanceConfig);

这与这样思考 ;传入 Backbone.Collection.extend(...) 的对象没有什么神奇之处。你只是传递一个普通的对象。当使用该对象作为参数调用 Backbone.Collection.extend 时,神奇的事情就会发生。

因此,对象方法 initialize 的 options 参数完全是完全正确的。与传入 new Store(...) 的内容不同。分配给initialize的函数定义了选项的范围。谁知道 new Store(options.store) 中引用的那个是在哪里定义的。它可以是window.options,也可以是在其他范围内定义的选项。如果未定义,您可能会收到错误话

虽如此,我只看到两三个战略选项(哦天哪,请原谅双关语!)。

每当您创建集合的新实例时,可以:

  1. 传递语言并让您的 Backbone 集合在需要时创建 new Store(..)
  2. 预先创建 Store,然后将特定的 Store 需求传递或提供给该实例(直接通过其构造函数,或者您可以让构造函数“查找”适当的预先创建的 Store)。
  3. 最后,我想您可以将创建存储的任务委托给另一个对象,并让它实现选项一或选项二。 (基本上是商店工厂/资源管理器之类的东西)。

您需要弄清楚的是这些策略中哪一种应该适合您。我从未使用过 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:

App.WordList = Backbone.Collection.extend({
  initialize : function(models, options){
      ....   
  },
  localStorage : new Store(options.store)
})

This is really little different from

var newInstanceConfig = {
  initialize : function(models, options){
      ....   
  },
  localStorage : new Store(options.store)
}
App.WordList = Backbone.Collection.extend(newInstanceConfig);

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 when Backbone.Collection.extend is invoked with that object as a parameter

Thus, the options parameter of the object method initialize is completely different that which is being passed in to new Store(...). The function being assigned initialize is defining the scope of options. Who knows where the one referred to in new Store(options.store) is defined. It could be window.options or it could be options defined in some other scope. If it's undefined, you're likely getting an error

That 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:

  1. Pass in the language and let your Backbone collection create the new Store(..) where needed.
  2. Pre-Create the Stores and either pass or give the specific Store want to that instance (either directly through its constructor or maybe you have your constructor "look-up" the appropriate pre-created Store).
  3. And finally, I guess you could delegate the task of creating stores to another object and have it implement either options one or two. (Basically a Store Factory/Resource Manager kinda thing).

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 this Store 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 a Storage constructor but nothing of a Store. So you might want to figure out that as well.

Edit #1: Nevermind, I see you mentioned where Store was defined.

迷途知返 2025-01-01 14:43:54

我通过创建一个方法来解决这个问题,该方法允许您在实例化后配置 localStorage:

var PageAssetCollection = Backbone.Collection.extend ({

initialize: <stuff>

model: <something>
...

setLocalStorage: function ( storageKey ) {
    this.localStorage = new Backbone.LocalStorage(storageKey),      
},
});

然后您可以在设置集合后设置 localStorage:

fooPageAssets = new PageAssetCollection();
fooPageAssets.setLocalStorage('bar'); 

I got around this by creating a method which allows you to configure the localStorage after instantiation:

var PageAssetCollection = Backbone.Collection.extend ({

initialize: <stuff>

model: <something>
...

setLocalStorage: function ( storageKey ) {
    this.localStorage = new Backbone.LocalStorage(storageKey),      
},
});

you can then set the localStorage after you have set up the collection:

fooPageAssets = new PageAssetCollection();
fooPageAssets.setLocalStorage('bar'); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文