toddmotto / ama

Ask me anything!
20 stars 3 forks source link

Angular Architecture : service placement #26

Closed timothylombrana closed 8 years ago

timothylombrana commented 8 years ago

Hey @toddmotto,

Don't know if you already have a similar question answered or a post but I'd like to know more about setting up large project architecture. For now specifically placing the service inside model(as a chained method), agree or disagree, why? Thanks, and cheers!

toddmotto commented 8 years ago

Hey, would you be able to provide some more info / maybe some architecture examples and code flow etc?

timothylombrana commented 8 years ago

Hey, yes certainly. Basically is the service in Angular considered the model? I only ask is because I've seen it used this way For example this is the way I define my service, but I'll include the way I've seen them written. Please let me know which is the best/preferred for all extensive purposes. Thanks!

//My example which is namely ExampleService.
function ExampleService (){
'use strict';
console.log('ExampleService')';
}

module.exports = function(ngModule){
ngModule.service('ExampleService', ExampleService);
};

//Others example which the service used as a model.
angular
.module('app')
.service('OthersExampleModel', function () {
    console.log('OthersExampleModel');
});
toddmotto commented 8 years ago

So, a model in my opinion is the data that is kept in sync inside the controller, but fetched from a service.

Runtime:

So really, a Service should provide you the data, but then inside the Controller, you either make a copy of it or assign the response from the Service to a Controller property. Hope that makes some sense!

timothylombrana commented 8 years ago

100%! Thanks for the clarification.

toddmotto commented 8 years ago

You're welcome, have a good'n :)