javascript - Factory vs Service object design Angular -
i'm creating web application allows users post messages (each message have associated tags in 1 categorize/filter messages). i'm having trouble understanding how design service these messages.
from understanding 5 possible functions create services in angular (providers, services, factories, values, , constants), services , factories being common.
a service wrapper around provider(), abstracting away of $provide configuration (i.e. $get). return value (a service object -> represents service instance) 1 use expose via controller.
a factory wrapper around service() , uses $injector new constructor function defined said service, believe returning constructor function opposed service object.
so service messages far looks (i arbitrarily chose go service(), have no real justification on why used on factory()). question add tags function, , how can write in way makes more sense point of view of service property;
two things i'd point out: 1) tags temporary array use hold possible tags give object, couldn't think of better way hold strings (tags) before pushing passing them message object.
2) of functions opening restfull api endpoints on node (they hit mongodb instance) i'll paste controller , node routes. think can save template, it's form user enter message , tags, tags has button binded controller , addtag() meant push each tag tags array.
angular .module('app') .service('messageservice', function () { this.messageserviceobject = { messages: [], tags: [] }; this.get = function (id) { return $http.get('/messages/' + id).then(function (res) { return res.data; }); }; this.getall = function () { return $http.get('/messages').success(function (data) { angular.copy(data, messageservice.messages); }); }; this.create = function (post) { return $http.post('/messages', post).success(function (data) { messageservice.messages.push(data); }); }; // test function this.create = function (blurb) { this.messageserviceobject.messages.push(blurb); }; });
// home controller
angular .module('app') .controller('homecontroller', homecontroller) homecontroller.$inject = ['messageservice']; function homecontroller() { var vm = this; vm.blurbs = messageservice.messageserviceobject.blurbs; vm.tags = []; vm.iscreating = false; vm.addmessage = addmessage; vm.addtag = addtag; function addmessage() { if (!vm.message || vm.message === '') { return; } messageservice.create({ message: vm.message, tags: vm.tags }); vm.message = ''; } function addtag() { if (!vm.tag || vm.tag === '') { return; } vm.tags.push(vm.tag); vm.tag = ''; } }
// api end points in node
router.get('/messages', function (req, res, next) { message.find(function (err, messages) { if (err) { return next(err); } res.json(messages); }); }); router.get('/messages:message', function (req, res, next) { req.message.populate('tags', function (err, blurb) { if (err) { return next(err); } res.json(req.message); }); }); router.post('/messages', function (req, res, next) { // create message object in memory var message= new message(req.body); // save databse message.save(function (err, message) { if (err) { return next(err); } res.json(message); }); });
Comments
Post a Comment