Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

model-debug.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('model', function(Y) {
  9. /**
  10. Attribute-based data model with APIs for getting, setting, validating, and
  11. syncing attribute values, as well as events for being notified of model changes.
  12. @submodule model
  13. @since 3.4.0
  14. **/
  15. /**
  16. Attribute-based data model with APIs for getting, setting, validating, and
  17. syncing attribute values, as well as events for being notified of model changes.
  18. In most cases, you'll want to create your own subclass of `Y.Model` and
  19. customize it to meet your needs. In particular, the `sync()` and `validate()`
  20. methods are meant to be overridden by custom implementations. You may also want
  21. to override the `parse()` method to parse non-generic server responses.
  22. @class Model
  23. @constructor
  24. @extends Base
  25. @since 3.4.0
  26. **/
  27. var GlobalEnv = YUI.namespace('Env.Model'),
  28. Lang = Y.Lang,
  29. YArray = Y.Array,
  30. YObject = Y.Object,
  31. /**
  32. Fired when one or more attributes on this model are changed.
  33. @event change
  34. @param {Object} changed Hash of change information for each attribute that
  35. changed. Each item in the hash has the following properties:
  36. @param {Any} changed.newVal New value of the attribute.
  37. @param {Any} changed.prevVal Previous value of the attribute.
  38. @param {String|null} changed.src Source of the change event, if any.
  39. **/
  40. EVT_CHANGE = 'change',
  41. /**
  42. Fired when an error occurs, such as when the model doesn't validate or when
  43. a sync layer response can't be parsed.
  44. @event error
  45. @param {Any} error Error message, object, or exception generated by the
  46. error. Calling `toString()` on this should result in a meaningful error
  47. message.
  48. @param {String} src Source of the error. May be one of the following (or any
  49. custom error source defined by a Model subclass):
  50. * `parse`: An error parsing a JSON response. The response in question will
  51. be provided as the `response` property on the event facade.
  52. * `validate`: The model failed to validate. The attributes being validated
  53. will be provided as the `attributes` property on the event facade.
  54. **/
  55. EVT_ERROR = 'error';
  56. function Model() {
  57. Model.superclass.constructor.apply(this, arguments);
  58. }
  59. Y.Model = Y.extend(Model, Y.Base, {
  60. // -- Public Properties ----------------------------------------------------
  61. /**
  62. Hash of attributes that have changed since the last time this model was
  63. saved.
  64. @property changed
  65. @type Object
  66. @default {}
  67. **/
  68. /**
  69. Name of the attribute to use as the unique id (or primary key) for this
  70. model.
  71. The default is `id`, but if your persistence layer uses a different name for
  72. the primary key (such as `_id` or `uid`), you can specify that here.
  73. The built-in `id` attribute will always be an alias for whatever attribute
  74. name you specify here, so getting and setting `id` will always behave the
  75. same as getting and setting your custom id attribute.
  76. @property idAttribute
  77. @type String
  78. @default `'id'`
  79. **/
  80. idAttribute: 'id',
  81. /**
  82. Hash of attributes that were changed in the last `change` event. Each item
  83. in this hash is an object with the following properties:
  84. * `newVal`: The new value of the attribute after it changed.
  85. * `prevVal`: The old value of the attribute before it changed.
  86. * `src`: The source of the change, or `null` if no source was specified.
  87. @property lastChange
  88. @type Object
  89. @default {}
  90. **/
  91. /**
  92. Array of `ModelList` instances that contain this model.
  93. When a model is in one or more lists, the model's events will bubble up to
  94. those lists. You can subscribe to a model event on a list to be notified
  95. when any model in the list fires that event.
  96. This property is updated automatically when this model is added to or
  97. removed from a `ModelList` instance. You shouldn't alter it manually. When
  98. working with models in a list, you should always add and remove models using
  99. the list's `add()` and `remove()` methods.
  100. @example Subscribing to model events on a list:
  101. // Assuming `list` is an existing Y.ModelList instance.
  102. list.on('*:change', function (e) {
  103. // This function will be called whenever any model in the list
  104. // fires a `change` event.
  105. //
  106. // `e.target` will refer to the model instance that fired the
  107. // event.
  108. });
  109. @property lists
  110. @type ModelList[]
  111. @default `[]`
  112. **/
  113. // -- Lifecycle Methods ----------------------------------------------------
  114. initializer: function (config) {
  115. this.changed = {};
  116. this.lastChange = {};
  117. this.lists = [];
  118. },
  119. // -- Public Methods -------------------------------------------------------
  120. /**
  121. Destroys this model instance and removes it from its containing lists, if
  122. any.
  123. If `options['delete']` is `true`, then this method also delegates to the
  124. `sync()` method to delete the model from the persistence layer, which is an
  125. asynchronous action. Provide a _callback_ function to be notified of success
  126. or failure.
  127. @method destroy
  128. @param {Object} [options] Sync options. It's up to the custom sync
  129. implementation to determine what options it supports or requires, if
  130. any.
  131. @param {Boolean} [options.delete=false] If `true`, the model will be
  132. deleted via the sync layer in addition to the instance being destroyed.
  133. @param {callback} [callback] Called when the sync operation finishes.
  134. @param {Error|null} callback.err If an error occurred, this parameter will
  135. contain the error. If the sync operation succeeded, _err_ will be
  136. `null`.
  137. @chainable
  138. **/
  139. destroy: function (options, callback) {
  140. var self = this;
  141. // Allow callback as only arg.
  142. if (typeof options === 'function') {
  143. callback = options;
  144. options = {};
  145. }
  146. function finish(err) {
  147. if (!err) {
  148. YArray.each(self.lists.concat(), function (list) {
  149. list.remove(self, options);
  150. });
  151. Model.superclass.destroy.call(self);
  152. }
  153. callback && callback.apply(null, arguments);
  154. }
  155. if (options && options['delete']) {
  156. this.sync('delete', options, finish);
  157. } else {
  158. finish();
  159. }
  160. return this;
  161. },
  162. /**
  163. Returns a clientId string that's unique among all models on the current page
  164. (even models in other YUI instances). Uniqueness across pageviews is
  165. unlikely.
  166. @method generateClientId
  167. @return {String} Unique clientId.
  168. **/
  169. generateClientId: function () {
  170. GlobalEnv.lastId || (GlobalEnv.lastId = 0);
  171. return this.constructor.NAME + '_' + (GlobalEnv.lastId += 1);
  172. },
  173. /**
  174. Returns the value of the specified attribute.
  175. If the attribute's value is an object, _name_ may use dot notation to
  176. specify the path to a specific property within the object, and the value of
  177. that property will be returned.
  178. @example
  179. // Set the 'foo' attribute to an object.
  180. myModel.set('foo', {
  181. bar: {
  182. baz: 'quux'
  183. }
  184. });
  185. // Get the value of 'foo'.
  186. myModel.get('foo');
  187. // => {bar: {baz: 'quux'}}
  188. // Get the value of 'foo.bar.baz'.
  189. myModel.get('foo.bar.baz');
  190. // => 'quux'
  191. @method get
  192. @param {String} name Attribute name or object property path.
  193. @return {Any} Attribute value, or `undefined` if the attribute doesn't
  194. exist.
  195. **/
  196. // get() is defined by Y.Attribute.
  197. /**
  198. Returns an HTML-escaped version of the value of the specified string
  199. attribute. The value is escaped using `Y.Escape.html()`.
  200. @method getAsHTML
  201. @param {String} name Attribute name or object property path.
  202. @return {String} HTML-escaped attribute value.
  203. **/
  204. getAsHTML: function (name) {
  205. var value = this.get(name);
  206. return Y.Escape.html(Lang.isValue(value) ? String(value) : '');
  207. },
  208. /**
  209. Returns a URL-encoded version of the value of the specified string
  210. attribute. The value is encoded using the native `encodeURIComponent()`
  211. function.
  212. @method getAsURL
  213. @param {String} name Attribute name or object property path.
  214. @return {String} URL-encoded attribute value.
  215. **/
  216. getAsURL: function (name) {
  217. var value = this.get(name);
  218. return encodeURIComponent(Lang.isValue(value) ? String(value) : '');
  219. },
  220. /**
  221. Returns `true` if any attribute of this model has been changed since the
  222. model was last saved.
  223. New models (models for which `isNew()` returns `true`) are implicitly
  224. considered to be "modified" until the first time they're saved.
  225. @method isModified
  226. @return {Boolean} `true` if this model has changed since it was last saved,
  227. `false` otherwise.
  228. **/
  229. isModified: function () {
  230. return this.isNew() || !YObject.isEmpty(this.changed);
  231. },
  232. /**
  233. Returns `true` if this model is "new", meaning it hasn't been saved since it
  234. was created.
  235. Newness is determined by checking whether the model's `id` attribute has
  236. been set. An empty id is assumed to indicate a new model, whereas a
  237. non-empty id indicates a model that was either loaded or has been saved
  238. since it was created.
  239. @method isNew
  240. @return {Boolean} `true` if this model is new, `false` otherwise.
  241. **/
  242. isNew: function () {
  243. return !Lang.isValue(this.get('id'));
  244. },
  245. /**
  246. Loads this model from the server.
  247. This method delegates to the `sync()` method to perform the actual load
  248. operation, which is an asynchronous action. Specify a _callback_ function to
  249. be notified of success or failure.
  250. If the load operation succeeds and one or more of the loaded attributes
  251. differ from this model's current attributes, a `change` event will be fired.
  252. @method load
  253. @param {Object} [options] Options to be passed to `sync()` and to `set()`
  254. when setting the loaded attributes. It's up to the custom sync
  255. implementation to determine what options it supports or requires, if any.
  256. @param {callback} [callback] Called when the sync operation finishes.
  257. @param {Error|null} callback.err If an error occurred, this parameter will
  258. contain the error. If the sync operation succeeded, _err_ will be
  259. `null`.
  260. @param {Any} callback.response The server's response. This value will
  261. be passed to the `parse()` method, which is expected to parse it and
  262. return an attribute hash.
  263. @chainable
  264. **/
  265. load: function (options, callback) {
  266. var self = this;
  267. // Allow callback as only arg.
  268. if (typeof options === 'function') {
  269. callback = options;
  270. options = {};
  271. }
  272. this.sync('read', options, function (err, response) {
  273. if (!err) {
  274. self.setAttrs(self.parse(response), options);
  275. self.changed = {};
  276. }
  277. callback && callback.apply(null, arguments);
  278. });
  279. return this;
  280. },
  281. /**
  282. Called to parse the _response_ when the model is loaded from the server.
  283. This method receives a server _response_ and is expected to return an
  284. attribute hash.
  285. The default implementation assumes that _response_ is either an attribute
  286. hash or a JSON string that can be parsed into an attribute hash. If
  287. _response_ is a JSON string and either `Y.JSON` or the native `JSON` object
  288. are available, it will be parsed automatically. If a parse error occurs, an
  289. `error` event will be fired and the model will not be updated.
  290. You may override this method to implement custom parsing logic if necessary.
  291. @method parse
  292. @param {Any} response Server response.
  293. @return {Object} Attribute hash.
  294. **/
  295. parse: function (response) {
  296. if (typeof response === 'string') {
  297. try {
  298. return Y.JSON.parse(response);
  299. } catch (ex) {
  300. this.fire(EVT_ERROR, {
  301. error : ex,
  302. response: response,
  303. src : 'parse'
  304. });
  305. return null;
  306. }
  307. }
  308. return response;
  309. },
  310. /**
  311. Saves this model to the server.
  312. This method delegates to the `sync()` method to perform the actual save
  313. operation, which is an asynchronous action. Specify a _callback_ function to
  314. be notified of success or failure.
  315. If the save operation succeeds and one or more of the attributes returned in
  316. the server's response differ from this model's current attributes, a
  317. `change` event will be fired.
  318. @method save
  319. @param {Object} [options] Options to be passed to `sync()` and to `set()`
  320. when setting synced attributes. It's up to the custom sync implementation
  321. to determine what options it supports or requires, if any.
  322. @param {Function} [callback] Called when the sync operation finishes.
  323. @param {Error|null} callback.err If an error occurred or validation
  324. failed, this parameter will contain the error. If the sync operation
  325. succeeded, _err_ will be `null`.
  326. @param {Any} callback.response The server's response. This value will
  327. be passed to the `parse()` method, which is expected to parse it and
  328. return an attribute hash.
  329. @chainable
  330. **/
  331. save: function (options, callback) {
  332. var self = this,
  333. validation = self._validate(self.toJSON());
  334. // Allow callback as only arg.
  335. if (typeof options === 'function') {
  336. callback = options;
  337. options = {};
  338. }
  339. if (!validation.valid) {
  340. callback && callback.call(null, validation.error);
  341. return self;
  342. }
  343. self.sync(self.isNew() ? 'create' : 'update', options, function (err, response) {
  344. if (!err) {
  345. if (response) {
  346. self.setAttrs(self.parse(response), options);
  347. }
  348. self.changed = {};
  349. }
  350. callback && callback.apply(null, arguments);
  351. });
  352. return self;
  353. },
  354. /**
  355. Sets the value of a single attribute. If model validation fails, the
  356. attribute will not be set and an `error` event will be fired.
  357. Use `setAttrs()` to set multiple attributes at once.
  358. @example
  359. model.set('foo', 'bar');
  360. @method set
  361. @param {String} name Attribute name or object property path.
  362. @param {any} value Value to set.
  363. @param {Object} [options] Data to be mixed into the event facade of the
  364. `change` event(s) for these attributes.
  365. @param {Boolean} [options.silent=false] If `true`, no `change` event will
  366. be fired.
  367. @chainable
  368. **/
  369. set: function (name, value, options) {
  370. var attributes = {};
  371. attributes[name] = value;
  372. return this.setAttrs(attributes, options);
  373. },
  374. /**
  375. Sets the values of multiple attributes at once. If model validation fails,
  376. the attributes will not be set and an `error` event will be fired.
  377. @example
  378. model.setAttrs({
  379. foo: 'bar',
  380. baz: 'quux'
  381. });
  382. @method setAttrs
  383. @param {Object} attributes Hash of attribute names and values to set.
  384. @param {Object} [options] Data to be mixed into the event facade of the
  385. `change` event(s) for these attributes.
  386. @param {Boolean} [options.silent=false] If `true`, no `change` event will
  387. be fired.
  388. @chainable
  389. **/
  390. setAttrs: function (attributes, options) {
  391. var idAttribute = this.idAttribute,
  392. changed, e, key, lastChange, transaction;
  393. options || (options = {});
  394. transaction = options._transaction = {};
  395. // When a custom id attribute is in use, always keep the default `id`
  396. // attribute in sync.
  397. if (idAttribute !== 'id') {
  398. // So we don't modify someone else's object.
  399. attributes = Y.merge(attributes);
  400. if (YObject.owns(attributes, idAttribute)) {
  401. attributes.id = attributes[idAttribute];
  402. } else if (YObject.owns(attributes, 'id')) {
  403. attributes[idAttribute] = attributes.id;
  404. }
  405. }
  406. for (key in attributes) {
  407. if (YObject.owns(attributes, key)) {
  408. this._setAttr(key, attributes[key], options);
  409. }
  410. }
  411. if (!YObject.isEmpty(transaction)) {
  412. changed = this.changed;
  413. lastChange = this.lastChange = {};
  414. for (key in transaction) {
  415. if (YObject.owns(transaction, key)) {
  416. e = transaction[key];
  417. changed[key] = e.newVal;
  418. lastChange[key] = {
  419. newVal : e.newVal,
  420. prevVal: e.prevVal,
  421. src : e.src || null
  422. };
  423. }
  424. }
  425. if (!options.silent) {
  426. // Lazy publish for the change event.
  427. if (!this._changeEvent) {
  428. this._changeEvent = this.publish(EVT_CHANGE, {
  429. preventable: false
  430. });
  431. }
  432. this.fire(EVT_CHANGE, {changed: lastChange});
  433. }
  434. }
  435. return this;
  436. },
  437. /**
  438. Override this method to provide a custom persistence implementation for this
  439. model. The default just calls the callback without actually doing anything.
  440. This method is called internally by `load()`, `save()`, and `destroy()`.
  441. @method sync
  442. @param {String} action Sync action to perform. May be one of the following:
  443. * `create`: Store a newly-created model for the first time.
  444. * `delete`: Delete an existing model.
  445. * `read` : Load an existing model.
  446. * `update`: Update an existing model.
  447. @param {Object} [options] Sync options. It's up to the custom sync
  448. implementation to determine what options it supports or requires, if any.
  449. @param {callback} [callback] Called when the sync operation finishes.
  450. @param {Error|null} callback.err If an error occurred, this parameter will
  451. contain the error. If the sync operation succeeded, _err_ will be
  452. falsy.
  453. @param {Any} [callback.response] The server's response. This value will
  454. be passed to the `parse()` method, which is expected to parse it and
  455. return an attribute hash.
  456. **/
  457. sync: function (/* action, options, callback */) {
  458. var callback = YArray(arguments, 0, true).pop();
  459. if (typeof callback === 'function') {
  460. callback();
  461. }
  462. },
  463. /**
  464. Returns a copy of this model's attributes that can be passed to
  465. `Y.JSON.stringify()` or used for other nefarious purposes.
  466. The `clientId` attribute is not included in the returned object.
  467. If you've specified a custom attribute name in the `idAttribute` property,
  468. the default `id` attribute will not be included in the returned object.
  469. @method toJSON
  470. @return {Object} Copy of this model's attributes.
  471. **/
  472. toJSON: function () {
  473. var attrs = this.getAttrs();
  474. delete attrs.clientId;
  475. delete attrs.destroyed;
  476. delete attrs.initialized;
  477. if (this.idAttribute !== 'id') {
  478. delete attrs.id;
  479. }
  480. return attrs;
  481. },
  482. /**
  483. Reverts the last change to the model.
  484. If an _attrNames_ array is provided, then only the named attributes will be
  485. reverted (and only if they were modified in the previous change). If no
  486. _attrNames_ array is provided, then all changed attributes will be reverted
  487. to their previous values.
  488. Note that only one level of undo is available: from the current state to the
  489. previous state. If `undo()` is called when no previous state is available,
  490. it will simply do nothing.
  491. @method undo
  492. @param {Array} [attrNames] Array of specific attribute names to revert. If
  493. not specified, all attributes modified in the last change will be
  494. reverted.
  495. @param {Object} [options] Data to be mixed into the event facade of the
  496. change event(s) for these attributes.
  497. @param {Boolean} [options.silent=false] If `true`, no `change` event will
  498. be fired.
  499. @chainable
  500. **/
  501. undo: function (attrNames, options) {
  502. var lastChange = this.lastChange,
  503. idAttribute = this.idAttribute,
  504. toUndo = {},
  505. needUndo;
  506. attrNames || (attrNames = YObject.keys(lastChange));
  507. YArray.each(attrNames, function (name) {
  508. if (YObject.owns(lastChange, name)) {
  509. // Don't generate a double change for custom id attributes.
  510. name = name === idAttribute ? 'id' : name;
  511. needUndo = true;
  512. toUndo[name] = lastChange[name].prevVal;
  513. }
  514. });
  515. return needUndo ? this.setAttrs(toUndo, options) : this;
  516. },
  517. /**
  518. Override this method to provide custom validation logic for this model.
  519. While attribute-specific validators can be used to validate individual
  520. attributes, this method gives you a hook to validate a hash of all
  521. attributes before the model is saved. This method is called automatically
  522. before `save()` takes any action. If validation fails, the `save()` call
  523. will be aborted.
  524. A call to `validate` that doesn't return anything (or that returns `null`)
  525. will be treated as a success. If the `validate` method returns a value, it
  526. will be treated as a failure, and the returned value (which may be a string
  527. or an object containing information about the failure) will be passed along
  528. to the `error` event.
  529. @method validate
  530. @param {Object} attributes Attribute hash containing all model attributes to
  531. be validated.
  532. @return {Any} Any return value other than `undefined` or `null` will be
  533. treated as a validation failure.
  534. **/
  535. validate: function (/* attributes */) {},
  536. // -- Protected Methods ----------------------------------------------------
  537. /**
  538. Duckpunches the `addAttr` method provided by `Y.Attribute` to keep the
  539. `id` attribute’s value and a custom id attribute’s (if provided) value
  540. in sync when adding the attributes to the model instance object.
  541. Marked as protected to hide it from Model's public API docs, even though
  542. this is a public method in Attribute.
  543. @method addAttr
  544. @param {String} name The name of the attribute.
  545. @param {Object} config An object with attribute configuration property/value
  546. pairs, specifying the configuration for the attribute.
  547. @param {boolean} lazy (optional) Whether or not to add this attribute lazily
  548. (on the first call to get/set).
  549. @return {Object} A reference to the host object.
  550. @chainable
  551. @protected
  552. **/
  553. addAttr: function (name, config, lazy) {
  554. var idAttribute = this.idAttribute,
  555. idAttrCfg, id;
  556. if (idAttribute && name === idAttribute) {
  557. idAttrCfg = this._isLazyAttr('id') || this._getAttrCfg('id');
  558. id = config.value === config.defaultValue ? null : config.value;
  559. if (!Lang.isValue(id)) {
  560. // Hunt for the id value.
  561. id = idAttrCfg.value === idAttrCfg.defaultValue ? null : idAttrCfg.value;
  562. if (!Lang.isValue(id)) {
  563. // No id value provided on construction, check defaults.
  564. id = Lang.isValue(config.defaultValue) ?
  565. config.defaultValue :
  566. idAttrCfg.defaultValue;
  567. }
  568. }
  569. config.value = id;
  570. // Make sure `id` is in sync.
  571. if (idAttrCfg.value !== id) {
  572. idAttrCfg.value = id;
  573. if (this._isLazyAttr('id')) {
  574. this._state.add('id', 'lazy', idAttrCfg);
  575. } else {
  576. this._state.add('id', 'value', id);
  577. }
  578. }
  579. }
  580. return Model.superclass.addAttr.apply(this, arguments);
  581. },
  582. /**
  583. Calls the public, overridable `validate()` method and fires an `error` event
  584. if validation fails.
  585. @method _validate
  586. @param {Object} attributes Attribute hash.
  587. @return {Object} Validation results.
  588. @protected
  589. **/
  590. _validate: function (attributes) {
  591. var error = this.validate(attributes);
  592. if (Lang.isValue(error)) {
  593. // Validation failed. Fire an error.
  594. this.fire(EVT_ERROR, {
  595. attributes: attributes,
  596. error : error,
  597. src : 'validate'
  598. });
  599. return {valid: false, error: error};
  600. }
  601. return {valid: true};
  602. },
  603. // -- Protected Event Handlers ---------------------------------------------
  604. /**
  605. Duckpunches the `_defAttrChangeFn()` provided by `Y.Attribute` so we can
  606. have a single global notification when a change event occurs.
  607. @method _defAttrChangeFn
  608. @param {EventFacade} e
  609. @protected
  610. **/
  611. _defAttrChangeFn: function (e) {
  612. var attrName = e.attrName;
  613. if (!this._setAttrVal(attrName, e.subAttrName, e.prevVal, e.newVal)) {
  614. Y.log('State not updated and stopImmediatePropagation called for attribute: ' + attrName + ' , value:' + e.newVal, 'warn', 'attribute');
  615. // Prevent "after" listeners from being invoked since nothing changed.
  616. e.stopImmediatePropagation();
  617. } else {
  618. e.newVal = this.get(attrName);
  619. if (e._transaction) {
  620. e._transaction[attrName] = e;
  621. }
  622. }
  623. }
  624. }, {
  625. NAME: 'model',
  626. ATTRS: {
  627. /**
  628. A client-only identifier for this model.
  629. Like the `id` attribute, `clientId` may be used to retrieve model
  630. instances from lists. Unlike the `id` attribute, `clientId` is
  631. automatically generated, and is only intended to be used on the client
  632. during the current pageview.
  633. @attribute clientId
  634. @type String
  635. @readOnly
  636. **/
  637. clientId: {
  638. valueFn : 'generateClientId',
  639. readOnly: true
  640. },
  641. /**
  642. A unique identifier for this model. Among other things, this id may be
  643. used to retrieve model instances from lists, so it should be unique.
  644. If the id is empty, this model instance is assumed to represent a new
  645. item that hasn't yet been saved.
  646. If you would prefer to use a custom attribute as this model's id instead
  647. of using the `id` attribute (for example, maybe you'd rather use `_id`
  648. or `uid` as the primary id), you may set the `idAttribute` property to
  649. the name of your custom id attribute. The `id` attribute will then
  650. act as an alias for your custom attribute.
  651. @attribute id
  652. @type String|Number|null
  653. @default `null`
  654. **/
  655. id: {value: null}
  656. }
  657. });
  658. }, '3.4.0' ,{requires:['base-build', 'escape', 'json-parse']});