This is a really interesting question, and I would like people to think about solutions. Theoretically, you can stick with Backbone models. Perhaps it has a performance cost. There is no reason this should not work.
Design your model layer without worrying about AngularJS. Then you will need to expand your models and add a listener to your initialization function, which will run $ rootScope. $ applies whenever a model changes, the same for any collection you can use. Something like:
angular.module('ng') .value('Backbone', Backbone) .factory('AngularModel', function(Backbone, $rootScope) { return Backbone.Model.extend({ initialize: function() { this.on('all', function() { if (!$rootScope.$$phase) { $rootScope.$apply(); } }); } }); }) .factory('AngularCollection', function(AngularModel, $rootScope) { return Backbone.Collection.extend({ model: AngularModel, initialize: function() { this.on('all', function() { if (!$rootScope.$$phase) { $rootScope.$apply(); } }); } }); }); function Main($scope, AngularCollection) { $scope.collection = new AngularCollection([{ name: "foo" }, { name: "bar" }, { name: "baz" }]); $scope.addModel = function(model) { $scope.collection.add(model); }; }
and presentation
<body ng-app ng-controller="Main"> <div ng-repeat="model in collection.models">{{model.get('name')}}</div> <form name="model_form" ng-submit="addModel(model)"> <fieldset> <legend>Add model</legend> <label for="">Name</label> <input type="text" ng-model="model.name" /> <input type="submit" /> </fieldset> </form> </body>
Some DEMO HERE
Now, in my opinion, AngularJS works better with raw js hashes. But if you need to move something to AngularJS from Backbone, this might be the solution if it already has a strong model layer.
EDIT: It can work without the expensive $ rootScope. $ apply,
mpm
source share