Skip to content Skip to sidebar Skip to footer

Managing Models Relations In 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?

Solution 1:

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

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

Solution 2:

Angular doesn't have a "data store" layer like ember-data, but you can integrate any existing ORM if you'd like to - What options are available for client-side JavaScript ORM?

Post a Comment for "Managing Models Relations In Angularjs"