0

I have a can.Model with defined findOne, findAll methods. Sometimes however it is required to create model instances from plain objects, e.g. these objects are bootstrapped in html as globals during initial page load.

The problem is that these instances are not merged with instances stored in can.Model.store object. Moreover they are not getting stored there while they have defined id attribute. Is it expected behaviour? What is the right pattern to create model instances bootstrapped in html as variables?

pheasant
  • 94
  • 6

1 Answers1

1

Only models that are bound to (e.g. having data displayed on the page) will be added to the store. The store is only used for keeping those models. If nobody is listening to model changes there is no need to keep them stored (in fact, it would create a memory leak). You can verify it like this:

var model = new MyModel({ name: 'David' });

model.bind('change', function() {});
Daff
  • 43,734
  • 9
  • 106
  • 120
  • Daff, thanks again. But what about the case, when constructed model is bound later. It may seems a little bit artificial, but it was actually the case with my code. Suppose, that you constructed a model and used its attributes in application logic, but displayed it later in popup widget. Between construction and usage in the template there was an ajax call, which resulted in a list of models on of which has the same id. In this case it will be replaced from the storage later, when popup is rendered. Here is a jsfiddle http://jsfiddle.net/alexboldakov/2pf2d2mf/14/. – pheasant Mar 02 '15 at 12:18
  • I understand the reason of not saving unbound models, it is clear. I just can't find the right way to prevent bound models from being thrown away from the model store. – pheasant Mar 02 '15 at 12:46
  • Good point, that might be worth looking into. Would you mind creating a [new issue](https://github.com/bitovi/canjs/issues/new) with the Fiddle? It's easier to keep track that way. – Daff Mar 02 '15 at 22:17