在角管理模式的关系 [英] Managing models relations in angular

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

问题描述

如何管理与角模型的关系?它是在灰烬很容易,但如何处理它的角度和不能产生一堆杂乱code的?

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

推荐答案

我使用 https://github.com / klederson / ModelCore / 这是非常简单和易于使用

I use https://github.com/klederson/ModelCore/ it's very simple and easy to use

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

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

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