I am very new to Angular, so hopefully I know enough to ask what seems like a reasonable design issue.
I draw some data through Angular, and use $ resource. Before bringing Angular to the project, I created a factory diagram function to create diagram objects from the json sample data that I just inserted into the view.
Now, when I use Angular, it forces to force the graphic functions in the "Chart" resource, in the style of Active Record, so I have one thing that can draw, save, update, etc.
While the advantage of this pattern is simplicity, the disadvantage is the constant connection with behavior. For example, it would be rather inconvenient if I wanted to save the diagram settings in local storage.
Having been bitten by AR enough times in my career already, I want to go with DM, leaving my chart object as is, and having the controller transfer the data from the resource to my chart.
But! My vague understanding of angularjs dependency injection suggests that I could create a resource, or some of them that could take a common persistence interface, is the correct word "scope"?
Example AR Strategy:
App.factory('Chart', [ '$resource', function($resource) { var chart = $resource('/api/charts/:id', { id: '@id' }); chart.draw = function() { ... } return chart } ]); App.controller('ChartsCtrl', [ '$scope', 'Chart', function($scope, Chart) { $scope.charts = Chart.query(function() { $.each($scope.charts, function(i, c) { c.draw() }) }) } ])
Example DM Strategy:
App.chart = function(resource) { return { draw: function() { ... } } } App.factory('ChartResource', [ '$resource', function($resource) { return $resource('/api/charts/:id', { id: '@id' }) } ]) App.controller('ChartsCtrl', [ '$scope', 'ChartResource', function($scope, ChartResource) { $scope.charts = $.map(ChartResource.query(), function(i, resource) { chart = App.chart(resource) chart.draw() return chart } } ])
I think there is a third way, however, that I do not see, because I do not quite understand how to use DI.
In other words, what is the AngularJS method for creating an object with save-and-replace strategies?