Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to override Model.get(attr) in Backbone.js?

Tags:

I'm using Backbone.js for the first time, and liking it so far. One thing I can't work out at the moment in dynamic attributes of models. For example, say I have a Person model, and I want to get their full name:

var Person = Backbone.Model.extend({   getFullName: function () {     return this.get('firstName') + ' ' + this.get('surname');   } }); 

Then I could do person.getFullName(). But I'd like to keep it consistent with the other getters, more like person.get('fullName'). I don't see how to do that without messily overriding Person#get. Or is that my only option?

This is what I've got so far for the overriding option:

var Person = Backbone.Model.extend({   get: function (attr) {     switch (attr) {     case 'fullName':       return this.get('firstName') + ' ' + this.get('surname');       break;     case 'somethingElse':       return this.doSomethingClever();       break;     default:       return Backbone.Model.prototype.get.call(this, attr);     }   } }); 

I suppose it's not terrible, but it seems there should be a better way.

like image 724
Skilldrick Avatar asked Jul 14 '11 15:07

Skilldrick


People also ask

How can we get the attribute value of a model in Backbone JS?

js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)

Why use Backbone JS?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.

How does Backbone JS work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).


2 Answers

Would this be simpler?

var Person = Backbone.Model.extend({   get: function (attr) {     if (typeof this[attr] == 'function')     {       return this[attr]();     }      return Backbone.Model.prototype.get.call(this, attr);   } }); 

This way you could also override existing attributes with functions. What do you think?

like image 119
bytespider Avatar answered Sep 24 '22 00:09

bytespider


I would think of attributes as the raw materials used by a model to provide answers to callers that ask the questions. I actually don't like having callers know too much about the internal attribute structure. Its an implementation detail. What if this structure changes?

So my answer would be: don't do it.

Create a method as you've done and hide the implementation details. Its much cleaner code and survives implementation changes.

like image 36
Bill Eisenhauer Avatar answered Sep 25 '22 00:09

Bill Eisenhauer