View Full Version : is using modelmgr to create instance of a model a bad practice.

17 Mar 2012, 12:42 PM
I recently saw a guy saying that using modelmgr to create model instances is a "bad practice". Here is what he is saying, I am speechless, Can anybody from Sencha wants to have a little say?

This answer actually considers what should be the behaviour when loading data in store, many things listed here is not used by questioner, but you should code in a manner that will not create bugs in future if you try to add new functionalities

1) why re-invent the wheel, if library is providing a function for doing something then why we should copy library code to achieve the same thing,

2) if we are providing model class to store config then store will handle creation of models, why use ModelMgr? (yeah i know sencha touch internally doing it, but whats the point of exposing that logic here)

3) loadData internally adds all models instances in to the store in a single operation so that will only fire 'datachanged' event once, so if user is listening for that particular event then he will properly get it, but your solution will add all models one by one which in turn will fire 'add' and 'datachanged' events for every model, which isn't good if user is listening to that event, because 'add' event should only be fired when we are adding extra models to already populated store

3) loadData also internally applies sorting and filtering to the records if sontOnLoad and filterOnLoad is true, which will fail in your case4) you are also calling 'sync' method after adding every record, i am not sure why you have used it here but from the documentation i can say that its used to synchronize contents between store and proxy, this is not needed when you do loadData

19 Mar 2012, 5:51 AM
It's ok to do it, it's not bad practice. Not all application needs are the same.

If you are using a store and want to add data to it then yes, I would simple pass in an object and let the store do the model instance creation.

However, you can use Models outside of a store, I quite often do this. Look at the API Docs, to load a model instance remotely you can use ModelMgr to get the model. It's very valid to do this.