backbone-associations-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +26 -0
- data/Rakefile +58 -0
- data/backbone-associations-rails.gemspec +19 -0
- data/lib/backbone-associations-rails.rb +10 -0
- data/lib/backbone-associations-rails/version.rb +7 -0
- data/vendor/assets/javascripts/backbone-associations.js +197 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Stafford Brunk
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# backbone-associations-rails [](http://badge.fury.io/rb/backbone-associations-rails)
|
2
|
+
|
3
|
+
This gem vendors the [Backbone-associations](https://github.com/dhruvaray/backbone-associations) library for easy usage in the Rails asset pipeline.
|
4
|
+
|
5
|
+
## Dependencies
|
6
|
+
|
7
|
+
```backbone-associations``` requires Backbone.js >= 0.9.2 and Underscore.js >= 1.3.1
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
group :assets
|
14
|
+
gem 'backbone-associations-rails'
|
15
|
+
end
|
16
|
+
|
17
|
+
Then require it in your javascript manifest file (```application.js``` by default)
|
18
|
+
|
19
|
+
//= reuqire backbone-associations
|
20
|
+
|
21
|
+
## Versioning
|
22
|
+
|
23
|
+
```backbone-associations-rails``` will track the version of ```backbone-associations```.
|
24
|
+
|
25
|
+
## Issues
|
26
|
+
For issues with ```backbone-associations```, please file an issue over at their [issue tracker](https://github.com/dhruvaray/backbone-associations/issues).
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
|
6
|
+
# Helper Functions
|
7
|
+
def name
|
8
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
9
|
+
end
|
10
|
+
|
11
|
+
def version
|
12
|
+
line = File.read("lib/#{name}/version.rb")[/^\s*VERSION\s*=\s*.*/]
|
13
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
14
|
+
end
|
15
|
+
|
16
|
+
def latest_tag
|
17
|
+
tags = JSON.parse(open('https://api.github.com/repos/dhruvaray/backbone-associations/tags').read)
|
18
|
+
tags.sort!{|a,b| b["name"] <=> a["name"]}
|
19
|
+
tags.first
|
20
|
+
end
|
21
|
+
|
22
|
+
namespace :backbone_associations do
|
23
|
+
|
24
|
+
desc "Fetches and displays the latest backbone-associations tag"
|
25
|
+
task :latest do
|
26
|
+
tag = latest_tag
|
27
|
+
puts "The latest backbone-associations tag is #{tag["name"]} with commit #{tag["commit"]["sha"]}"
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Updates the vendored backbone-associations version to the latest tag"
|
31
|
+
task :update do
|
32
|
+
tag = latest_tag
|
33
|
+
|
34
|
+
# Pull attributes we need
|
35
|
+
name = tag["name"]
|
36
|
+
sha = tag["commit"]["sha"]
|
37
|
+
|
38
|
+
# Cleanup the name
|
39
|
+
name.gsub!(/^v/, '')
|
40
|
+
name.gsub!(/-/, '.')
|
41
|
+
|
42
|
+
if name == version
|
43
|
+
puts "Gem version #{version} matches the latest backbone-associations version #{name}"
|
44
|
+
exit
|
45
|
+
end
|
46
|
+
|
47
|
+
# Update marionette
|
48
|
+
puts "Updating backbone-associations..."
|
49
|
+
url = "https://raw.github.com/dhruvaray/backbone-associations/#{sha}/lib/backbone-associations.js"
|
50
|
+
Dir.chdir './vendor/assets/javascripts' do
|
51
|
+
`curl -O #{url}`
|
52
|
+
end
|
53
|
+
|
54
|
+
# Update version file
|
55
|
+
puts "Updating version.rb..."
|
56
|
+
`sed -i "" "s/ VERSION = '.*'/ VERSION = '#{name}'/g" lib/backbone-associations-rails/version.rb`
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'backbone-associations-rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "backbone-associations-rails"
|
8
|
+
gem.version = Backbone::Associations::Rails::VERSION
|
9
|
+
gem.authors = ["Stafford Brunk"]
|
10
|
+
gem.email = ["stafford.brunk@gmail.com"]
|
11
|
+
gem.description = %q{Backbone-associations provides a way of specifying 1:1 and 1:N relationships between Backbone models. Additionally, parent model instances (and objects extended from Backbone.Events) can listen in to CRUD events initiated on any children - in the object graph - by providing an appropriately qualified event path name.}
|
12
|
+
gem.summary = %q{Packages the backbone.associations library for use with the asset pipline}
|
13
|
+
gem.homepage = "https://github.com/wingrunr21/backbone-associations-rails"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,197 @@
|
|
1
|
+
//
|
2
|
+
// Backbone-associations.js 0.1.0
|
3
|
+
//
|
4
|
+
// (c) 2012 Dhruva Ray, Jaynti Kanani
|
5
|
+
//
|
6
|
+
// Backbone-associations may be freely distributed under the MIT license; see the accompanying LICENSE.txt.
|
7
|
+
//
|
8
|
+
// Depends on [Backbone](https://github.com/documentcloud/backbone) (and thus on [Underscore](https://github.com/documentcloud/underscore/) as well)
|
9
|
+
//
|
10
|
+
// A complete [Test & Benchmark Suite](../test/test-suite.html) is included for your perusal.
|
11
|
+
|
12
|
+
// Initial Setup
|
13
|
+
// --------------
|
14
|
+
(function() {
|
15
|
+
// The top-level namespace. All public Backbone classes and modules will be attached to this.
|
16
|
+
// Exported for the browser and CommonJS.
|
17
|
+
var _, Backbone;
|
18
|
+
if ( typeof window === 'undefined' ) {
|
19
|
+
_ = require( 'underscore' );
|
20
|
+
Backbone = require( 'backbone' );
|
21
|
+
exports = module.exports = Backbone;
|
22
|
+
}
|
23
|
+
else {
|
24
|
+
_ = window._;
|
25
|
+
Backbone = window.Backbone;
|
26
|
+
exports = window;
|
27
|
+
}
|
28
|
+
// Backbone.AssociatedModel
|
29
|
+
// --------------
|
30
|
+
|
31
|
+
//Add `Many` and `One` relations to Backbone Object
|
32
|
+
Backbone.Many = "Many";
|
33
|
+
Backbone.One = "One";
|
34
|
+
//Define `AssociatedModel` (Extends Backbone.Model)
|
35
|
+
Backbone.AssociatedModel = Backbone.Model.extend({
|
36
|
+
//Define relations with Associated Model
|
37
|
+
relations: undefined,
|
38
|
+
//Set a hash of model attributes on the object,
|
39
|
+
//firing `"change"` unless you choose to silence it.
|
40
|
+
//It maintains relations between models during the set operation. It also bubbles up child events to the parent.
|
41
|
+
set: function( key, value, options ) {
|
42
|
+
var attributes,processedRelations,tbp;
|
43
|
+
// Duplicate backbone's behavior to allow separate key/value parameters, instead of a single 'attributes' object.
|
44
|
+
if ( _.isObject( key ) || key == null ) {
|
45
|
+
attributes = key;
|
46
|
+
options = value;
|
47
|
+
}
|
48
|
+
else {
|
49
|
+
attributes = {};
|
50
|
+
attributes[ key ] = value;
|
51
|
+
}
|
52
|
+
// Extract attributes and options.
|
53
|
+
options || (options = {});
|
54
|
+
if(!attributes) return;
|
55
|
+
if (options.unset) for (attr in attributes) attributes[attr] = void 0;
|
56
|
+
//Check for existence of relations in this model
|
57
|
+
if(this.relations){
|
58
|
+
//Iterate over `this.relations` and `set` model and collection values
|
59
|
+
_.each(this.relations ,function(relation){
|
60
|
+
if (attributes[relation.key] != undefined ){
|
61
|
+
//Get value of attribute with relation key in `val`
|
62
|
+
var val = getValue(attributes,relation.key);
|
63
|
+
//Get class if relation is stored as a string
|
64
|
+
relation.relatedModel && _.isString(relation.relatedModel) && (relation.relatedModel = eval(relation.relatedModel));
|
65
|
+
relation.collectionType && _.isString(relation.collectionType) && (relation.collectionType = eval(relation.collectionType));
|
66
|
+
//Track reference change of associated model for `change:attribute` event
|
67
|
+
var refChanged = false;
|
68
|
+
//If `relation` defines model as associated collection...
|
69
|
+
if(relation.type === Backbone.Many){
|
70
|
+
//`relation.collectionType` should be instance of `Backbone.Collection`
|
71
|
+
if(relation.collectionType && !relation.collectionType.prototype instanceof Backbone.Collection){
|
72
|
+
throw new Error( 'collectionType must inherit from Backbone.Collection' );
|
73
|
+
}
|
74
|
+
//Create new `Backbone.Collection` with `collectionType`
|
75
|
+
if(!this.attributes[relation.key]){
|
76
|
+
this.attributes[relation.key] = relation.collectionType ? new relation.collectionType() : this._createCollection(relation.relatedModel);
|
77
|
+
refChanged = true;
|
78
|
+
}
|
79
|
+
// Get all models if `val` is already instanceof `Backbone.Collection`
|
80
|
+
if(val instanceof Backbone.Collection){
|
81
|
+
val = val.models;
|
82
|
+
}
|
83
|
+
//Resetting new Collection with new value and options
|
84
|
+
this.attributes[relation.key].reset(val,options);
|
85
|
+
}
|
86
|
+
//If `relation` defines model as associated model...
|
87
|
+
else if(relation.type == Backbone.One && relation.relatedModel){
|
88
|
+
//Create New `Backbone.Model` using `relation.relatedModel` if `attributes` is not null
|
89
|
+
if(!this.attributes[relation.key]){
|
90
|
+
//If `val` is already instance of `AssociatedModel`, reserve `relation.key` for `Backbone.Model.prototype.set`
|
91
|
+
if(val instanceof Backbone.AssociatedModel) return;
|
92
|
+
this.attributes[relation.key] = new relation.relatedModel();
|
93
|
+
refChanged = true;
|
94
|
+
}
|
95
|
+
//If the new attributes is a smaller subset, then use the default values for that attribute - if available.
|
96
|
+
else{
|
97
|
+
var opt = {};
|
98
|
+
var defaults = getValue(this.attributes[relation.key], 'defaults');
|
99
|
+
_.each(this.attributes[relation.key].attributes,function(value,key){
|
100
|
+
!_.has(val,key) && (opt[key] = (defaults ? defaults[key] : void 0));
|
101
|
+
});
|
102
|
+
this.attributes[relation.key].set(opt,{silent:true});
|
103
|
+
}
|
104
|
+
//Set `val` to model with options
|
105
|
+
this.attributes[relation.key].set(val,options);
|
106
|
+
}
|
107
|
+
//Add proxy events to respective parents
|
108
|
+
this.attributes[relation.key].off("all");
|
109
|
+
this.attributes[relation.key].on("all",function(events){this.trigger(events,_.rest(arguments))},this);
|
110
|
+
//If reference has changed, trigger `change:attribute` event
|
111
|
+
refChanged && this.trigger('change:'+relation.key,options);
|
112
|
+
//Create a local `processedRelations` array to store the relation key which has been processed.
|
113
|
+
//We cannot use `this.relations` because if there is no value defined for `relation.key`, it will not get processed by either Backbone `set` or the `AssociatedModel` set
|
114
|
+
!processedRelations && (processedRelations=[]);
|
115
|
+
if(_.indexOf(processedRelations,relation.key)===-1){
|
116
|
+
processedRelations.push(relation.key);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
},this);
|
120
|
+
};
|
121
|
+
if(processedRelations){
|
122
|
+
//Find attributes yet to be processed - `tbp`
|
123
|
+
tbp = {};
|
124
|
+
for(var key in attributes){
|
125
|
+
if(_.indexOf(processedRelations,key)===-1){
|
126
|
+
tbp[key] = attributes[key];
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}
|
130
|
+
//Set all `attributes` to `tbp`
|
131
|
+
else{
|
132
|
+
tbp = attributes;
|
133
|
+
}
|
134
|
+
//Returns results for `Backbone.Model.prototype.set`
|
135
|
+
return Backbone.Model.prototype.set.call( this, tbp , options);
|
136
|
+
},
|
137
|
+
//Returns New `collection` of type `relation.relatedModel`
|
138
|
+
_createCollection: function(type) {
|
139
|
+
var collection;
|
140
|
+
if ( type && type.prototype instanceof Backbone.AssociatedModel ) {
|
141
|
+
//Creates new `Backbone.Collection` and defines model class
|
142
|
+
collection = new Backbone.Collection();
|
143
|
+
collection.model = type;
|
144
|
+
}
|
145
|
+
else{
|
146
|
+
throw new Error( 'type must inherit from Backbone.AssociatedModel' );
|
147
|
+
}
|
148
|
+
return collection;
|
149
|
+
},
|
150
|
+
//The JSON representation of the model.
|
151
|
+
toJSON : function(){
|
152
|
+
//Get json representation from `Backbone.Model.prototype.toJSON`
|
153
|
+
var json = Backbone.Model.prototype.toJSON.apply( this, arguments );
|
154
|
+
//If `this.relations` is defined, iterate through each `relation` and added it's json representation to parents' json representation
|
155
|
+
if(this.relations){
|
156
|
+
_.each(this.relations ,function(relation){
|
157
|
+
if(this.attributes[relation.key]){
|
158
|
+
json[relation.key] = (this.attributes[relation.key].cid !== this.cid) ? this.attributes[relation.key].toJSON() : void 0;
|
159
|
+
}
|
160
|
+
},this);
|
161
|
+
}
|
162
|
+
return json;
|
163
|
+
},
|
164
|
+
//deep `clone` the model.
|
165
|
+
clone : function(){
|
166
|
+
//Get shallow clone from `Backbone.Model.prototype.clone`
|
167
|
+
var cloneObj = Backbone.Model.prototype.clone.apply( this, arguments );
|
168
|
+
//If `this.relations` is defined, iterate through each `relation` and `clone`
|
169
|
+
if(this.relations){
|
170
|
+
_.each(this.relations ,function(relation){
|
171
|
+
if(this.attributes[relation.key]){
|
172
|
+
var sourceObj = cloneObj.attributes[relation.key];
|
173
|
+
if(sourceObj instanceof Backbone.Collection){
|
174
|
+
//Creates new `collection` using `relation`
|
175
|
+
var newCollection = relation.collectionType ? new relation.collectionType() : this._createCollection(relation.relatedModel);
|
176
|
+
//Added each `clone` model to `newCollection`
|
177
|
+
sourceObj.each(function(model){
|
178
|
+
newCollection.add(model.clone());
|
179
|
+
});
|
180
|
+
cloneObj.attributes[relation.key] = newCollection;
|
181
|
+
}
|
182
|
+
else if(sourceObj instanceof Backbone.Model){
|
183
|
+
cloneObj.attributes[relation.key] = sourceObj.clone();
|
184
|
+
}
|
185
|
+
}
|
186
|
+
},this);
|
187
|
+
}
|
188
|
+
return cloneObj;
|
189
|
+
}
|
190
|
+
});
|
191
|
+
// Duplicate Backbone's behavior. To get a value from a Backbone object as a property
|
192
|
+
// or as a function.
|
193
|
+
var getValue = function(object, prop) {
|
194
|
+
if (!(object && object[prop])) return null;
|
195
|
+
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
|
196
|
+
};
|
197
|
+
})();
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: backbone-associations-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stafford Brunk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-07 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Backbone-associations provides a way of specifying 1:1 and 1:N relationships
|
15
|
+
between Backbone models. Additionally, parent model instances (and objects extended
|
16
|
+
from Backbone.Events) can listen in to CRUD events initiated on any children - in
|
17
|
+
the object graph - by providing an appropriately qualified event path name.
|
18
|
+
email:
|
19
|
+
- stafford.brunk@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- backbone-associations-rails.gemspec
|
30
|
+
- lib/backbone-associations-rails.rb
|
31
|
+
- lib/backbone-associations-rails/version.rb
|
32
|
+
- vendor/assets/javascripts/backbone-associations.js
|
33
|
+
homepage: https://github.com/wingrunr21/backbone-associations-rails
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.23
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Packages the backbone.associations library for use with the asset pipline
|
57
|
+
test_files: []
|