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?

1
  • Can you give some example? In AngularJS you manipulate with javascript objects, so you can directly reference other objects and then $watch them (don't forget to set true to second parameter). Also there is Dependency injection so you can inject depended objects (note value method of module) Commented Feb 5, 2013 at 14:53

2 Answers 2

2

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())
    }
  });
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.