Re: UniqueModel.js

From disqus.com, 1 Month ago, written in JavaScript, viewed 3 times. This paste is a reply to utils.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/41743218 Embed
Download Paste or View Raw
  1. define('core/UniqueModel',[
  2.     'underscore',
  3. ], function (
  4.     _
  5. ) {
  6.     'use strict';
  7.  
  8.     // This is a factory function that enforces uniqueness of model instances.
  9.     // It stores all instances by their ID (actual key should be specified via
  10.     // idAttribute on a model).
  11.     //
  12.     // Example: Registering a unique model.
  13.     //
  14.     //  UniqueModel.addType("User", User);
  15.     //
  16.     // Example: Creating a new instance.
  17.     //
  18.     //  var first = new UniqueModel(User, { id: 1, name: "Anton" });
  19.     //  var second = new UniqueModel(User, { id: 1, name: "Anton" });
  20.     //  first === second; // true
  21.     //
  22.     // If an instance already exists, this function will simply update its
  23.     // attributes.
  24.     //
  25.     // Example: Declaring a collection.
  26.     //
  27.     //  var UsersCollection = Backbone.Collection.extend({
  28.     //      model: _.bind(UniqueModel, {}, User),
  29.     //      ...
  30.     //  });
  31.     function UniqueModel(Model, attrs, options) {
  32.         var pool = UniqueModel.pool(Model);
  33.         var key = attrs && attrs[Model.prototype.idAttribute];
  34.  
  35.         if (!key)
  36.             return new Model(attrs, options);
  37.  
  38.         var instance = UniqueModel.get(Model, key);
  39.         if (instance)
  40.             pool[key].set(attrs);
  41.         else
  42.             pool[key] = new Model(attrs, options);
  43.  
  44.         return pool[key];
  45.     }
  46.  
  47.     UniqueModel.pool = {};
  48.  
  49.     UniqueModel.pool = function (Model) {
  50.         var pool = UniqueModel.pool[Model.__type__];
  51.         if (!pool)
  52.             throw new Error('Model not registered. Use UniqueModel.addType');
  53.         return pool;
  54.     };
  55.  
  56.     UniqueModel.get = function (Model, key) {
  57.         return UniqueModel.pool(Model)[key];
  58.     };
  59.  
  60.     UniqueModel.set = function (Model, model) {
  61.         var pool = UniqueModel.pool(Model);
  62.         var key = model && model.get(Model.prototype.idAttribute);
  63.  
  64.         if (!key)
  65.             return model;
  66.  
  67.         var instance = UniqueModel.get(Model, key);
  68.         if (instance)
  69.             instance.set(model.attributes);
  70.         else
  71.             instance = pool[key] = model;
  72.  
  73.         return instance;
  74.     };
  75.  
  76.     UniqueModel.addType = function (name, obj) {
  77.         if (obj.__type__ && UniqueModel.pool[name])
  78.             return;
  79.  
  80.         obj.__type__ = name;
  81.         UniqueModel.pool[name] = {};
  82.     };
  83.  
  84.     UniqueModel.boundModel = function (Model) {
  85.         var constructor = _.bind(UniqueModel, UniqueModel, Model);
  86.         constructor.prototype = Model.prototype;
  87.         return constructor;
  88.     };
  89.  
  90.     UniqueModel.wrap = UniqueModel.boundModel;
  91.  
  92.     return UniqueModel;
  93. });
  94.  
  95.  
  96. // https://c.disquscdn.com/next/next-core/core/UniqueModel.js

Replies to Re: UniqueModel.js rss

Title Name Language When
Re: Re: time.js disqus.com javascript 1 Month ago.

Reply to "Re: UniqueModel.js"

Here you can reply to the paste above