Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backbone.rel

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone.rel - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

28

backbone.rel.js

@@ -123,2 +123,26 @@ /**

/**
* Gets an attribute over a relation
*
* @param {String} relation
* @param {String} attribute
* @param {<A>} default_value
* @return {<A>}
*/
function relGet(relation, attribute, default_value) {
var rel = this.rel(relation);
if (rel) {
if (_.isArray(rel)) {
return _.map(rel, function (r) {
return r.get(attribute);
});
} else {
return rel.get(attribute);
}
} else {
return default_value || null;
}
}
/**
* Computes and gets the relationship

@@ -153,5 +177,5 @@ *

_.extend(Backbone.Model.prototype, {rel: rel('Model')});
_.extend(Backbone.Collection.prototype, {rel: rel('Collection')});
_.extend(Backbone.Model.prototype, {rel: rel('Model'), relGet: relGet});
_.extend(Backbone.Collection.prototype, {rel: rel('Collection'), relGet: relGet});
}());

2

package.json
{
"name": "backbone.rel",
"description": "Lightweight relationship assistant for your backbone applications",
"version": "0.1.2",
"version": "0.2.0",
"author": "Pau ramon <masylum@gmail.com>",

@@ -6,0 +6,0 @@ "contributors": ["Maxim lolls <collsmaxim@gmail.com>"],

@@ -35,2 +35,12 @@ # Backbone.Rel

## Accessing attributes inside the relations
Most of the errors on working with relational data is when a relation is not met and we try to access a attribute
of that inexistant model. For instance, we have a task that belong to a project and we want to get that projects name.
```js
task.relGet('project', 'name', 'Unknown project'); // It will show the project name or 'Unkown project'
user.relGet('project.tasks', 'name', []); // It will return all tasks names or []
```
## Example

@@ -37,0 +47,0 @@

@@ -88,9 +88,9 @@ module.exports = (function () {

projects = new Collections.Projects([
{id: 0, owner_id: 0}
, {id: 1, owner_id: 0}
{id: 0, owner_id: 0, name: 'project1'}
, {id: 1, owner_id: 0, name: 'project2'}
]);
users = new Collections.Users([
{id: 0, project_id: 0}
, {id: 1, project_id: 1}
{id: 0, project_id: 0, name: 'user1'}
, {id: 1, project_id: 1, name: 'user2'}
, {id: 2 }

@@ -100,17 +100,17 @@ ]);

tasks = new Collections.Tasks([
{id: 0}
, {id: 1, user_id: 1}
, {id: 2, user_id: 0}
, {id: 3, user_id: 1}
, {id: 4, user_id: 0}
, {id: 5, user_id: 1}
{id: 0, name: 'task1'}
, {id: 1, user_id: 1, name: 'task2'}
, {id: 2, user_id: 0, name: 'task3'}
, {id: 3, user_id: 1, name: 'task4'}
, {id: 4, user_id: 0, name: 'task5'}
, {id: 5, user_id: 1, name: 'task6'}
]);
comments = new Collections.Comments([
{id: 1, task_id: 1}
, {id: 2, task_id: 1}
, {id: 3, task_id: 2}
, {id: 4, task_id: 2}
, {id: 5, task_id: 3}
, {id: 6, task_id: 4}
{id: 1, task_id: 1, name: 'comment1'}
, {id: 2, task_id: 1, name: 'comment2'}
, {id: 3, task_id: 2, name: 'comment3'}
, {id: 4, task_id: 2, name: 'comment4'}
, {id: 5, task_id: 3, name: 'comment5'}
, {id: 6, task_id: 4, name: 'comment6'}
]);

@@ -117,0 +117,0 @@

@@ -43,2 +43,8 @@ /*global it, describe, before, beforeEach*/

});
it('returns the tasks names for a given user', function () {
assert.deepEqual(users.get(0).relGet('tasks', 'name'), ['task3', 'task5']);
assert.deepEqual(users.get(1).relGet('tasks', 'name'), ['task2', 'task4', 'task6']);
assert.deepEqual(users.get(2).relGet('tasks', 'name'), []);
});
});

@@ -74,2 +80,9 @@

});
it('returns the project name for a given user', function () {
assert.equal(users.get(0).relGet('project', 'name'), 'project1');
assert.equal(users.get(1).relGet('project', 'name'), 'project2');
assert.deepEqual(users.get(2).relGet('project', 'name', 'hihi'), 'hihi');
assert.deepEqual(users.get(2).relGet('project', 'name'), null);
});
});

@@ -84,2 +97,6 @@

});
it('returns the comment names of all user tasks', function () {
assert.deepEqual(users.get(0).relGet('tasks.comments', 'name'), ['comment3', 'comment4', 'comment6']);
});
});

@@ -86,0 +103,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc