1

how you manage models relations with AngularJS? It is very easy in Ember, but how to deal with it in AngularJS and not produce bunch of messy code?

4

2 回答 2

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())
    }
  });
于 2013-06-11T23:06:41.700 回答
0

Angular 没有像 ember-data 这样的“数据存储”层,但如果您愿意,可以集成任何现有的 ORM -哪些选项可用于客户端 JavaScript ORM?

于 2013-02-05T23:04:56.187 回答