Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : Why use Models.model()

$
0
0
Just one use case: say you have two Models Artist and Album. 

Updated Album data arrives with nested artist data, and you want artist data (artist attribute) be converted into Artist instance.

Using attributes plugin you do:

  1. var Artist = can.Model({..})

  2. var Album = can.Model({
  3.       
  4.       attributes: {
  5.             artist: 'Artist'
  6.       },
  7.       convert: {
  8.             Artist: function(val){return Artist.model(val)}
  9.       }

  10. },{
  11. })
at first glance instead of 
  1. return Artist.model(val)
you could do just:
  1. return new Artist(val)
But the the problem is that new Artist(val) will just replace artist attribute with new instance (new _cid) and replace instance  in Artist.store. If you prefer to keep working with exiting instance you want to use Artist.model(val)

Viewing all articles
Browse latest Browse all 3491

Trending Articles