Skip to content

Commit

Permalink
Began adding the relationships collection to the Dataset model. Ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Jul 12, 2011
1 parent e7d780d commit b4392ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
25 changes: 20 additions & 5 deletions lib/model.js
Expand Up @@ -71,6 +71,12 @@ CKAN.Model = function ($, _, Backbone, undefined) {
// Model objects
Model.Dataset = Model.Base.extend({
constructor: function Dataset() {
// Define an key/model mapping for child relationships. These will be
// managed as a Backbone collection when setting/getting the key.
this.children = {
resources: Model.Resource,
relationships: Model.Relationship
};
Model.Base.prototype.constructor.apply(this, arguments);
},

Expand All @@ -81,12 +87,9 @@ CKAN.Model = function ($, _, Backbone, undefined) {
set: function (attributes, options) {
var resources, validated;

// If not yet defined set the resources collection. This will be done when
// If not yet defined set the child collections. This will be done when
// set is called for the first time in the constructor.
if (!this.get('resources')) {
this.attributes.resources = new Backbone.Collection();
this.attributes.resources.model = Model.Resource;
}
this._createRelationships();

if (attributes && attributes.resources) {
if (!(attributes.resources instanceof Backbone.Collection)) {
Expand All @@ -105,6 +108,18 @@ CKAN.Model = function ($, _, Backbone, undefined) {
return validated;
},

// Checks to see if our model instance has Backbone collections defined for
// child keys. If they do not exist it creates them.
_createRelationships: function () {
_.each(this.children, function (Model, key) {
if (!this.get(key)) {
this.attributes[key] = new Backbone.Collection();
this.attributes[key].model = Model;
}
}, this);
return this;
},

// Manages the one to many relationship between resources and the dataset.
// Accepts an array of Resources (ideally model instances but will convert
// object literals into resources for you). New models will be added to the
Expand Down
18 changes: 16 additions & 2 deletions test/model-test.js
Expand Up @@ -73,13 +73,13 @@ test(".set({resources: []})", function () {
resources = dataset.get('resources'),
newResources = new Backbone.Collection();

this.spy(dataset, '_createRelationships');
this.spy(dataset, '_updateResources');

equals(resources.constructor, Backbone.Collection, 'The resources attribute should be a Backbone collection');
equals(resources.model, CKAN.Model.Resource, 'Should create Resource instances');
equals(resources.length, 0, 'The resources collection should be empty');

dataset.set({});
equals(dataset._createRelationships.callCount, 1, 'Expected dataset._createRelationships() to have been called');
ok(!dataset._updateResources.calledOnce, 'Expected collection._updateResources() NOT to have been called');

dataset.set({resources: newResources});
Expand All @@ -93,6 +93,20 @@ test(".set({resources: []})", function () {
ok(dataset._updateResources.calledTwice, 'Expected collection._updateResources() to have been called');
});

test("._createRelationships()", function () {
var dataset = new CKAN.Model.Dataset(), attrs, returned;
attrs = dataset.attributes = {};

returned = dataset._createRelationships();

ok(dataset, returned, 'Expected it to return itself');
equals(attrs.resources.constructor, Backbone.Collection, 'The resources attribute should be a Backbone collection');
equals(attrs.resources.model, CKAN.Model.Resource, 'Should create Resource instances');

equals(attrs.relationships.constructor, Backbone.Collection, 'The resources attribute should be a Backbone collection');
equals(attrs.relationships.model, CKAN.Model.Relationship, 'Should create Resource instances');
});

test("._updateResources()", function () {
var dataset = new CKAN.Model.Dataset(),
resources = dataset.get('resources'),
Expand Down

0 comments on commit b4392ad

Please sign in to comment.