Last active
December 15, 2015 11:58
-
-
Save jdkanani/5256463 to your computer and use it in GitHub Desktop.
RequireJS shim to use Backbone-associations.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require.config({ | |
paths: { | |
'jquery': 'vendor/jquery', | |
// ----------------------------------- | |
// Use AMD versions of Backbone/Underscore : https://github.com/amdjs | |
'underscore': 'vendor/underscore-amd', | |
'backbone': 'vendor/backbone-amd', | |
// ----------------------------------- | |
'backbone-associations': 'vendor/backbone-associations' | |
}, | |
// Define shim-config | |
// For more details : http://requirejs.org/docs/api.html#config-shim | |
shim: { | |
'underscore': { | |
exports: '_' | |
}, | |
'backbone': { | |
deps: ["underscore", "jquery"], | |
exports: "Backbone" | |
}, | |
// Define dependency for Backbone-associations | |
'backbone-associations': ["backbone"] | |
} | |
}); | |
define([ | |
'jquery', | |
'underscore', | |
'backbone', | |
//Add requirement for Backbone-associations module | |
'backbone-associations' | |
], function ($, _, Backbone) { | |
var Location = Backbone.AssociatedModel.extend({ | |
defaults: { | |
add1 : "", | |
add2 : null, | |
zip : "", | |
state : "" | |
} | |
}); | |
var Project = Backbone.AssociatedModel.extend({ | |
relations: [ | |
{ | |
type: Backbone.Many,//nature of the relation | |
key: 'locations', //attribute of Project | |
relatedModel:Location //AssociatedModel for attribute key | |
} | |
], | |
defaults: { | |
name : "", | |
number : 0, | |
locations : [] | |
} | |
}); | |
var Employee = Backbone.AssociatedModel.extend({ | |
relations: [ | |
{ | |
type: Backbone.One, | |
key: 'manager', | |
// Self reference | |
relatedModel: Backbone.Self | |
//OR | |
//define relatedModel as function | |
/*relatedModel: function() { | |
return Employee; | |
}*/ | |
} | |
], | |
defaults: { | |
fname: '', | |
lname: '', | |
age: -1, | |
sex: 'M', | |
manager: null | |
} | |
}); | |
return { | |
'Location': Location, | |
'Project': Project, | |
'Employee': Employee | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment