管理AngularJS中的模型关系

时间:2013-02-05 13:36:20

标签: javascript orm angularjs

如何管理与AngularJS的模型关系?在Ember中很容易,但是如何在AngularJS中处理它而不会产生一堆乱码?

2 个答案:

答案 0 :(得分:2)

我使用https://github.com/klederson/ModelCore/它非常简单易用

var ExampleApp = angular.module('ExampleApp', ['ModelCore']); //injecting ModelCore

ExampleApp.factory("Users",function(ModelCore) {
  return ModelCore.instance({
    $type : "Users", //Define the Object type
    $pkField : "idUser", //Define the Object primary key
    $settings : {
      urls : {
        base : "http://myapi.com/users/:idUser",
      }
    },
    $myCustomMethod : function(info) { //yes you can create and apply your own custom methods
        console.log(info);
    }
  });
});

然后我只需要注入我的控制器并使用它

function MainCrtl($scope, Users) {
  //Setup a model to example a $find() call
  $scope.AllUsers = new Users();

  //Get All Users from the API
  $scope.AllUsers.$find().success(function() {
    var current;
    while(current = $scope.AllUsers.$fetch()) { //fetching on masters object
      console.log("Fetched Data into Master Object",$scope.AllUsers.$toObject()) //reading fetched from master
      //or just get the fetched object itself
      console.log("Real fetched Object",current.$toObject())
    }
  });

答案 1 :(得分:0)

Angular没有像ember-data这样的“数据存储”图层,但如果您愿意,可以集成任何现有的ORM - What options are available for client-side JavaScript ORM?