管理AngularJS中的模型关系 [英] Managing models relations in AngularJS

查看:64
本文介绍了管理AngularJS中的模型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

我用的 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);
    }
  });
});

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

And then i just need to inject into my controller and use it

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())
    }
  });

这篇关于管理AngularJS中的模型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆