百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

backbone.uniquemodel

> 编程语言
开源

Backbone.js 插件,用于在整个应用程序中确保独特的模型实例

98 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Backbone.js 插件,用于在整个应用程序中确保独特的模型实例

Backbone.UniqueModel

Backbone.UniqueModel ensures unique model instances across your application. It will also synchronize model data between windows/iframes using localStorage.

Usage

Instantiating models

When creating a new model, if that model is already being tracked, you will be returned the original model instance.

javascript
var UniqueUser = Backbone.UniqueModel(User);

var first  = new UniqueUser({ id: 1, name: 'Jean Grey' });
var second = new UniqueUser({ id: 1, name: 'Jean Summers' });

first === second; // true
first.get('name') === 'Jean Summers'; // true

UniqueModel will also update the attributes of the instance to reflect the latest state.

Working with collections

Backbone.UniqueModel also guarantees that instances created through a collection (e.g. via fetch) are also unique.

javascript
var UserCollection = Backbone.Collection.extend({
  model: UniqueUser
});

var users = new UserCollection([
  { id: 2, name: 'Henry McCoy' },
  { id: 3, name: 'Bobby Drake' }
]);

var user = new UniqueUser({ id: 2, name: 'Henry McCoy' });
user === users.get(2); // true

Window sync via localStorage

If enabled, UniqueModel will attempt to ensure uniqueness of model instances across windows using localStorage.

javascript
// Window 1
var UniqueUser = Backbone.UniqueModel(User, 'User', 'localStorage');
var logan1 = new UniqueUser({ id: 4, name: 'Logan' });

// Window 2
var UniqueUser = Backbone.UniqueModel(User, 'User', 'localStorage');
var logan2 = new UniqueUser({ id: 4, name: 'Logan', power: 'Healing' });

// Back to Window 1
logan1.get('power'); // Healing

Add event

It's possible for completely new models to become available through localStorage sync. To be notified of new models, subscribe to the uniquemodel.add event on your UniqueModel class.

For example, you can use this event to automatically add new models to your collections:

javascript
UniqueUser.on('uniquemodel.add', function (model) {
  userCollection.add(model);
});

Destroy event

If a model is destroyed in one window, the destroy event will be called on that model in any other open windows.

In Backbone, collections automatically remove any models that trigger destroy events. So there's nothing for you to do here — just know that it happens automatically.

javascript
// Window 1
userCollection.add(logan1);

// Window 2
logan2.destroy(); // Triggers 'destroy' event in Window 1

// Back to Window 1
userCollection.where({ name: 'Logan' }).length === 0; // Removed from set

Demo

Bundled in this repository is a version of TodoMVC that has been modified to use UniqueModel. It's a good demonstration of UniqueModel's window syncing abilities. Open up the demo in multiple windows, and observe your changes propagate instantly between each window intance.

You can try the demo live on GitHub, or you can run it yourself from the repository.

If you're curious, this demo was done by adding only 2 lines of code:

  • Making Todo as a UniqueModel class (link)
  • Binding the TodoCollection to the uniquemodel.add event (link)

Running Tests

The unit tests need to be served from a web server; they cannot be accessed from local filesystem. This is because localStorage isn't shared between file:// resources in Chrome, which causes the sync tests to fail in that browser.

If you have Python installed (pre-installed on OS X), you can just use SimpleHTTPServer:

$ python -m SimpleHTTPServer 8000

Then open http://localhost:8000/tests/ and you're off to the races.

Browser support

UniqueModel has been verified working in the following browsers:

  • Internet Explorer 8+ (localStorage sync requires IE9+)
  • Chrome 25
  • Safari 6
  • Firefox 18

IE8 and localStorage sync

Despite implementing webstorage, IE8's onstorage event doesn't communicate what data changed. There are workarounds, which I plan to explore in a future version.

Acknowledgments

Backbone.UniqueModel is written by Ben Vinegar, based on work from Anton Kovalyov and Burak Yigit Kaya.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言