Role of the model in angularjs

53 views
Skip to first unread message

Jens Hoffmann

unread,
Oct 21, 2014, 7:41:40 AM10/21/14
to ang...@googlegroups.com
hello, 

in my application there is a rest api which gets a product object. 

{
    "Product": {
        "productId": 181124961,
        "erpNumber": "1231124",
        "price": 34.99

    }
}

My question is now is it ok to extend this model like so, or is it better to create an additional layer for that (wrapper viewmodel): 


{
    "Product": {
        "productId": 181124961,
        "erpNumber": "1231124",
        "price": 34.99,
        "_chosenOptions": {124214, 1241241}, // own properties marked with "_"
        "_selectedQuantity": 1

    }
}



best regards,

jens



Nicholas Smith

unread,
Oct 21, 2014, 5:13:03 PM10/21/14
to ang...@googlegroups.com
One issue to watch out for is if you try to send those objects back to the server through the rest API, you might have an issue with those new fields being sent to your API. Depending on how your back-end is implemented it might just ignore the fields, but it could cause errors.

Eric Eslinger

unread,
Oct 21, 2014, 5:31:07 PM10/21/14
to ang...@googlegroups.com
That kind of depends in general. As Nicholas pointed out, you may end up posting those transient view values back to the server for persisting, which could be ignored or could throw an error or (worse of all) get persisted if you're going fully schemaless (but at that point, you have a pretty big problem with over-trusting).

The other issue you can run into is with a model cache. Personally, I end up cacheing XHR responses, so Profile.get(1) will only incur one $http request per lifecycle, but that means if I store view variables on the profile object, those view variables end up being persisted if you click away and see the same object in a new context.

Personally, I store no view variables on models. What I do, especially if I have to ng-repeat over a list of items where each item has some transient view state information (like expanded or hidden, rather than the model stuff like productId), is define a custom directive for displaying the item. Sometimes, these directives only get used in one place (but I usually end up finding new uses for them). That way, I can pass in the item to display (Profile or Document or whatever) as an '=' scope (usually) assignment, and set view variables in the directive's isolate scope. 

It's a little more work, but ultimately leads to a nice separation of concerns with my code.

e

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Jens Hoffmann

unread,
Nov 6, 2014, 10:52:24 AM11/6/14
to ang...@googlegroups.com
Thank you for your answer. I think I will create an wrapper view model to be more flexible. Creating a directive is not a possible solution in my situatioin. (The data is not passed back to the server) 

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/83eJbQBQvuQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages