Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should views set model data?

Tags:

backbone.js

I'm just trying to figure out a probably simple question.

Should views set model data directly or only call model methods that change their own data?

like image 236
boom Avatar asked Oct 03 '11 06:10

boom


People also ask

Should view be aware of model?

A view knows what the model is. That is, it should know what the model type is, what the model contains or by some means know how to use the model to display what is necessary. If we have a page called UserProfile then the view knows that it should display the user's name, email address, age and favourite website.

Does a ViewModel need a model?

A ViewModel may and in many cases does use multiple Models. It is itself a "Model" of your view. Consider a profile screen that a user enters their personal information including address.

How is model data used in view?

How to use the Model in View? Create “Index. cshtml” page in Home folder inside the View. To access the product model in view first, we need to import the Product model namespace then we can use the product model in view.

Does the view interact with the model?

The important thing is that the View and the Model never interact with each other. The only interaction that takes place between them is through the Controller. This means the logic of the application and the interface never interacts with each other, which makes writing complex applications easier.


2 Answers

like everything else in software development, "it depends".

if you're using form inputs in your views and you just need to get the data from those inputs in to the models, set the data directly. You can do this any number of ways, including "change" events from the input fields, like this:

MyView = Backbone.View.extend({
  events: {
    "change #name", "setName"
  },

  setName: function(e){
    var val = $(e.currentTarget).val();
    this.model.set({name: val});
  }
});

On the other hand, if you're kicking off business logic and other code that happens to set the data in the model (but really only does this as part of the business logic), then you should call a method on the model.

A "state machine" would be a good example of when you would do this. Or, in an image gallery that I wrote, I had some logic around the selection of an image for display. If an image is already selected, don't select it again. I put this logic in a method on my image model:

Image = Backbone.Model.extend({
  select: function(){
    if (!this.get("selected")){
      this.set({selected: true});
    }
  }
});

As illustrated here, I like to run by the simple rule that if I have zero logic around the call to set, then I set it directly from wherever I am. If there is any logic related to the model, around the set, then I put it in the model.

Either way, when you do want to set data, you should use the set method. Bypassing this and setting the model's attributes directly through model.attributes will prevent a lot of Backbone's code from firing and potentially cause problems for you.

like image 192
Derick Bailey Avatar answered Jan 04 '23 06:01

Derick Bailey


That depends on your programming style. I always set them directly. If the Law of Demeter sounds good to you and you're into object orientation (and possibly with a Java/Microsoft -stack background), then the style would be to create getter/setter methods.

If you on the other hand is in the "Size is the Enemy" camp (I also recommend Jeff Atwood's comments) the you certainly should set model data directly (as I hinted before, I'm in this camp myself).

That being said, Backbone.js models already have getter and setter methods .get and .set. You should not manipulate the .attributes member directly. I'm not as sure you shouldn't read from it, I tend to do that now and then and haven't run problems because of that yet.

like image 35
Jacob Oscarson Avatar answered Jan 04 '23 06:01

Jacob Oscarson