Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to allow naming of the model in the ViewModel #73

Closed
Naddiseo opened this issue Jul 30, 2013 · 1 comment
Closed

Option to allow naming of the model in the ViewModel #73

Naddiseo opened this issue Jul 30, 2013 · 1 comment

Comments

@Naddiseo
Copy link
Contributor

I'm probably doing something wrong, but in case I'm not, could there be an option to specify what attribute name the "model" is.
Currently this is model which can cause problems if the model itself has a field labelled model

var Car = Backbone.Model.extend({
   attributes : { 'year' : 2000, 'model' : '', 'make' : '' }
});

var CarViewModel = kb.ViewModel.extend({
   constructor : function(model) {
      kb.ViewModel.prototype.constructor.call(this, model);
      // this.model() refers to the model, instead of model.get('model').
   }
});

var vm = new CarViewModel(new Car({ year : 2012, model : 'Caravan', make : 'Dodge'}));

Here's a fiddle demonstrating this: http://jsfiddle.net/tbajy/

A possible fix for this would be to have an option that allows one to specify what attribute under the ViewModel the model should be.

@kmalakoff
Copy link
Owner

It is a little complicated to change the name of the model accessor and would make inconsistencies across view models.

Instead, you can use the internals property:

var Car = Backbone.Model.extend({});

var CarViewModel = kb.ViewModel.extend({
   constructor : function(model) {
      kb.ViewModel.prototype.constructor.call(this, model, {internals: ['model']});
      // this.model() refers to the model, instead of model.get('model').
   }
});

var model = new Car({ year : 2012, model : 'Caravan', make : 'Dodge'});
var vm = new CarViewModel(model);

vm._model() == 'Caravan'; // true
vm.model() == model; // true

kmalakoff added a commit that referenced this issue Sep 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants