From 424292e9e610b4a126b8f44619af8e482f9522b9 Mon Sep 17 00:00:00 2001 From: arjon_jason_castro Date: Thu, 23 Jun 2016 10:48:06 +0800 Subject: [PATCH 1/3] Add feature relations declaration using `$relations` property --- src/angular-activerecord.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/angular-activerecord.js b/src/angular-activerecord.js index 42331d1..4315cdf 100644 --- a/src/angular-activerecord.js +++ b/src/angular-activerecord.js @@ -89,6 +89,26 @@ angular.module('ActiveRecord', []).factory('ActiveRecord', ['$http', '$q', '$par if (options.urlRoot) { this.$urlRoot = options.urlRoot; } + + /* + * Set the declared relations on the model + */ + var instance = this; + var relations = {}; + angular.forEach(_result(this, '$relations'), function (def, relation) { + var key = def.key||relation; + relations[relation] = angular.isObject(instance[key]) && !angular.isArray(instance[key]) ? + new def.model(instance[key]) : + []; + + if (angular.isArray(relations[relation])) { + angular.forEach(instance[key], function (object) { + relations[relation].push(new def.model(object)); + }); + } + }); + + angular.extend(this, relations); }, /** From 678e9b4cc1b9f59fc2a5376a1e56248dece69638 Mon Sep 17 00:00:00 2001 From: arjon_jason_castro Date: Thu, 23 Jun 2016 11:10:19 +0800 Subject: [PATCH 2/3] Change variable to prop instead of key --- src/angular-activerecord.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/angular-activerecord.js b/src/angular-activerecord.js index 4315cdf..b9d4865 100644 --- a/src/angular-activerecord.js +++ b/src/angular-activerecord.js @@ -96,13 +96,13 @@ angular.module('ActiveRecord', []).factory('ActiveRecord', ['$http', '$q', '$par var instance = this; var relations = {}; angular.forEach(_result(this, '$relations'), function (def, relation) { - var key = def.key||relation; - relations[relation] = angular.isObject(instance[key]) && !angular.isArray(instance[key]) ? - new def.model(instance[key]) : + var prop = def.prop||relation; + relations[relation] = angular.isObject(instance[prop]) && !angular.isArray(instance[prop]) ? + new def.model(instance[prop]) : []; if (angular.isArray(relations[relation])) { - angular.forEach(instance[key], function (object) { + angular.forEach(instance[prop], function (object) { relations[relation].push(new def.model(object)); }); } From e77f2ade9071075817bc9b5d5cb5d1b34d273c84 Mon Sep 17 00:00:00 2001 From: Arjon Jason Castro Date: Thu, 23 Jun 2016 11:24:42 +0800 Subject: [PATCH 3/3] Add Defining Relations section in README --- Readme.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Readme.md b/Readme.md index b85ab22..1a7a48f 100644 --- a/Readme.md +++ b/Readme.md @@ -67,6 +67,71 @@ angular-activerecord is a [Backbone.Model](http://backbonejs.org/#Model) inspire }); ``` +### Defining Relations (Single-loaded from server) +This section is for single loaded nested data. If one of the property of a model is another relational model, it can be declared in `$relations` property. + +In the example below, there is an array property named `comments`, and each comment object will be automatically an instance of the `Comment` model. This also works for object properties. + +Sample Nested Object of a Task model: +```js +var dataFromServer = { + id: 1, + name: "Learn ActiveRecord", + comments: [ + { + id: 1, + text: "Woow! Cool!" + },{ + id: 2, + text: "Awesome!" + } + ] +}; + +var task = new Task(dataFromServer); +console.log(task.comments); // array of Comment models +``` +Models: +```js +app.factory('Task', function (ActiveRecord, Comment) { + + return ActiveRecord.extend({ + // ...some codes before + $relations: { + comments: { + model: Comment + } + } + }); +}); + +app.factory('Comment', function (ActiveRecord, Comment) { + return ActiveRecord.extend({ + // ...some codes + }); +}); +``` + +You can also rename the relation property instead of `comments`. + +```js +app.factory('Task', function (ActiveRecord, Comment) { + + return ActiveRecord.extend({ + // ...some codes before + $relations: { + coolComments: { + model: Comment, + prop: comments + } + } + }); +}); + +// This relation is now accessible via +console.log(Task.coolComments); // array of Comment models +``` + ### Fetching and saving data. ```js module('myApp').controller('TaskCtrl', function ($scope, Task, $document) {