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?
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.
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 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With