Notes on Supermodel

What is Supermodel ?

Supermodel is a special kind of Model that provides the model instances tracking for Backbone. Model instances with the same "id" derive from the same Supermodel class and could be treated as the same model by Supermodel. This is a kind of tracking that reduce unnecessary model copies. Models that shared with the same "super" ancient are stored within a Backbone.Collection and accessed from Model.all() function. Say if SModel = Supermodel.Model.extend(); then all instances of SModel are putted into the SModel.all() collection. It could raise some problems while you want to make these instances a collection (talk about it later). 

A Supermodel class can also be inherited from another class like ModelB = ModelA.extend(); then ModelB owns properties and method of ModelA now. Do not rub out possess of the parent Model while you want to add some properties. 

Like Backbone-relational, Supermodel provides "Has One" or "Has Many" associations. Models reach out each other through a property that concat the name of the relationship with "_id" suffix.

Why not using Backbone-relationship ? 1. BR takes 60+kb spaces while Supermodel just takes 20kb. 2. BR must be declared globally that cannot work with common.js or AMD.  


Tricks on using Supermodel

1. Supermodel promises that each instance is saved into a predefined Collection for tracking so model.collection has to be fixed with that. This could be a nightmare when working with some other backbone plugins (ie. LocalStorage Adapter) say you create a collection for some Supermodel instances and set a localStorage property, and model.collection would never point to it!!! (I've live through my all-night headache debugging). 

2. Another problem is associations get broken up as you override initialize(). This issue has been raised on github and solved by running "Supermodel.Model.prototype.initialize.apply(this, arguments);" at first in initialize().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值