javascript - Using John Papa's AngularJS style guide, what is the proper way to declare data objects? -
let's have angularjs data service makes call server , returns object can extended additional methods. example, assume following function part of angularjs service nerddinner.
function getdinner(dinnerid) { return $http.get('api/dinner/' + dinnerid) .then(loaddinnercomplete) .catch(loaddinnerfailed); function loaddinnercomplete(response) { return new dinner(response.data); } }
what's best practice place define dinner class? factory in separate file? define in nerddinner service? or define in getdinner class (assuming that's method can create dinners)?
i did not find specific reference creating objects in style guide, forgive me if it's covered , missed it.
edit decided accept jeroen's answer because matched needs rather simple use case. however, daniel's answer pure gold, , should not overlooked. if chose extend functionality of dto simple crud or additional server based operations, $resource great approach.
where place business entities (like dinner) not explicitly mentioned in john papa's style guide (afaik).
if want go route (using business entities , placing logic there), should give each entity own factory:
(function() { 'use strict'; angular .module('mydinner') .factory('dinner', dinnerfactory); function dinnerfactory() { dinner.prototype.eat = eat; dinner.prototype.cancel = cancel; return dinner; // constructor function dinner (data) { // example: this.time = data.time; this.location = data.location; } // methods function eat() { // ... } function cancel() { // ... } } })();
then can inject them controller or maybe other service objects, using dinner
, , create new dinner
object using new dinner(data)
.
you use service, john papa discourages services, because similar factories.
Comments
Post a Comment