Reputation: 3374
I have a form which gets fields added dynamically when clicked on add field button. Now for that new field, i need to have a separate view as it has some functionality attached. I am aware that there is a concept of subviews.
My question is that when should i choose a subview over the view and should i in my case?
Upvotes: 1
Views: 114
Reputation: 1993
Firstly , check this fiddle i have created : here
This serves as an example for a separate view / sub view concept.
Now, building on the example and discussing the sub view vs single view option , if you see the example , i create a sub view for each link i add , by doing this :
var listItem = new printView({ model: model}); //create a new view
$('#list').append(listItem.render().el); //append that view to my main view
this helps me in a lot of ways :-
I have a view for each model
I can associate actions to each view and they take care of them individually
Modularity
If you go for single view approach , you face 1 problem upfront is , when i click or remove (any action which you define) on a particular element , how do i get which model is it ?
Solution for the above is , you can associate the cid with each element and when that element is clicked or removed (any action which you define) you can go a getByCid()
to get the appropriate model.
Personally i would go with sub view or separate view approach since that gives a lot more flexibility and readability to your code.
Thank you.
Upvotes: 1