4 ms·
I'd lean towards never, unless you have a case with really special semantics. For run-of-the-mill attribute massaging, I'd just define a new method on the mode
by jashkenas 15y ago
I'd lean towards never, unless you have a case with really special semantics.
For run-of-the-mill attribute massaging, I'd just define a new method on the model specifically for that purpose. eg.
book.loadFromAmazon(amazonJSON)
- bct 15y agoHmmm, interesting. Here's a concrete example: I've got an 'Organization' model and a 'Person' model. I want to use them interchangeably in some templates, so I need to be able to get their names in a consistent way. An Organization just has a 'name', but a Person has a 'first_name' and a 'last_name'. I've been using a 'displayName' method to encapsulate the difference; would it make more sense to do something like: var Person = Backbone.Model.extend({ initialize: function() { var name = this.get('first_name') + ' ' + this.get('last_name'); this.set({ name: name }, { silent: true }); } });
- crescentfresh 15y ago.displayName() seems like a perfect encapsulation IMO.