Where does model manipulation logic go in AngularJS? -
i'm building application fetches calendar data via ajax call @ $rootscope. use object in various controllers, , need able parse appropriately, there various objects nested within it. should store logic parse it? i've had in root controller $rootscope functions, feel use case factory. however, doesn't seem can (nor feel idea to) access factory methods templates.
at moment have following factory:
angular.module('services',[]). factory('datamanipulation', function(){ return{ getperson: function(peopleobj, userid){ //since each user has unique id, returns array 1 element, // simplify view code, there's [0] @ end var person= peopleobj.filter(function(element, index, array) { if (array[index].cwid == userid) { return true; } })[0]; return person; } }; }); and following in controller:
angular.module('app.controllers',[]). controller('rootctrl', ['$rootscope', '$http', 'datamanipulation', function($rootscope, $http, datamanipulation) { $rootscope.getperson = function(peopleobj, userid){ return datamanipulation.getperson(peopleobj, userid); } } ]); and works, i'm not sure if i'm following best practices.
so, should these functions parse model in rootscope or somewhere else?
your post confusing (you need parse object? :s) use service or provider or factory (implementory variations of same thing) achieve describing. allow avoid use of $rootscope entirely , inject service / factory controllers use required.
$scope model (or rather storage device model, there dangers associated storing data directly against $scope i.e $scope.x = 1) in context of angular application, , $rootscope 'global' parent used managing scopes internally (as understand, may wrong , try confirm this) , emitting events @ global scope, can used quite conveniently store data - though should avoided possible. imo idea deliver model angular app in near usable state possible manipulation required model within angular app purely presentational.
Comments
Post a Comment