Can backbone model is having attribute backbone model simple array - backbone.js

Backbone model attributes can be string, objects, backbone collection etc. But is it possible that an attribute is an array of backbone model?
Ex:
var Model - Backbone model
Model.get('charts') - Simple array [chart], here chart is the backbone model
Here my question is that ideally Backbone model attribute is if having array then it should be backbone collection or can be simple array of backbone Model?

Technically there is nothing preventing you from doing that. However, backbone features such as model events won't propagate up to your parent model, since backbone doesn't support nested model/collections out of the box. So you don't get any "special" benefits by making a model property backbone collection instead of a normal array.

Related

Backbone-Event: add array of models to collection

If I add an array of models to a collection, is backbone calling callback for each model inside of this array an add-event?
I was wandering, because my view, listening to add-events of this collection, get's rendered that often, although I'm just adding one array of models to the collection.
Yes look at the documentation.

How to append new model to view in backbone.js?

On adding new model to my collection I don't want the view to be refreshed but I want the model to be appended to my collection and be appended to the view. SO i shouldn't render the view again. Is it possible via Backbone.js? How should I proceed ?
You could have a view that handles the whole layout, and a view that represents a single model. You would make the render method of the latter to return the HTML of a single model, and append the result to a list using the main view. You have a great example of this in the Addy Osmani's book "Developing Backbone.js applications". Take a look at the section where he explains how he renders each task to the todo list of his Todo App, if I understood well, your problem is solved in there.

Parsing updated collection content, hiding new model view instances

I have a Backbone view which represents a collection. When this collection is synced with the server, and new models are added to the collection, I would like to hide all the view instances that represent these new models in the collection, whilst continuing to display the old models' view instances. How can I do this?
I'm assuming you're using a Marionette.CollectionView or Marionette.CompositeView here, right? That being the case, you are trying to prevent these from showing the newly added models, when the come back from the server and are added to your collection, right?
I can see a few different ways of doing this, most of which begin at the same place: a collection that filters based on an attribute that tells you whether or not to show the model. You would need to have some piece of data on the models that tells you wether to show them or not... I'm not sure what this would look like off-hand, but once you have the logic set up for this, I think the rest of it becomes easier.
A Proxy Collection
My preferred method of handling this in the CompositeView or CollectionView would be to create the view instance with a proxy collection - one that is filtered based on the value to show or hide the models.
I've talked about proxy collections before, and I have a working JSFiddle to show how to build them, here: http://jsfiddle.net/derickbailey/7tvzF/
You would set up a filtered collection like this, and then send the filtered collection to your actual view instance:
var filtered = new FilteredCollection(myOriginalCollection);
filtered.filter({
showThisOne: true
});
var view = new MyCompositeView({
collection: filtered
});
myOriginalCollection.fetch();
From here, it comes down to how you want to manage the filtering and fetch/sync of the original collection. But I think the over-all solution hinges on the proxy/decorator collection and being able to filter the collection down to only the list of items that you want.
Views in Bbone are not automatically updated when the underlying model/collection is changed. You have to explicitly listen for events: change/destroy for models and add/change/remove/reset for collections and call render().
As WiredPrairie suggests, if you've registered during view initialization for example to listenTo() any of these events and explicitly render(), you can always use stopListening() to reverse the effect.
An alternative, in case it's a one-of case of suppressing the view from showing the changes, is to check manually in your view's render() which models have been changed and use the previous state of the changed attributes to avoid showing the new values. Check out: model.hasChanged() and model.previousAttributes() as well as the options.previousModels in case of a collection reset.

How do I reference a model from within a backbone.js view?

I am new to backbone.js and attempting to use patterns that I have used in other languages. Some of them work and some have fallen quite flat. My questions is this - how should I reference my model from within a view. For example, I have 5 identicals views that I passed a model from a collection of models. When the user clicks one of the 5 views, I need it to use the model that it was created with. Right now, I am getting null for this.model anytime the user clicks the view.
You need to pass the model in the view constructor as stated in the oficial doc http://backbonejs.org/#View-constructor
then you can access the model from the view with just this.model
if your getting undefined and you have passed the model in the view's constructor, maybe is because of the context when the callback function is being called,
have you checked that?

backbonejs menu from models belonging to different collection

I am developing a Backbonejs app using the excellent Marionette plugin.
I have a big navigation view consisting of <a> tags referencing various collections. An example will better explain this:
Cars
Car A
...
Car Z
Books
Book A
...
Book Z
Each block is a collection of models. Eg. CarList, BookList
How do i best architect the Menuview so that, whenever any of the model changes in any of the collection the Menu view is rerendered?
Maybe you could try to use a Composite View for your menu view and a Collection View for every sub-folder of your menu. Composite and Collection Views are made in a way that they will add/remove automatically a child element when a model is added/remove to the collection.
For more information you can check here: http://lostechies.com/derickbailey/2012/04/05/composite-views-tree-structures-tables-and-more/

Resources