backbone_extensions 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
@@ -2,120 +2,123 @@
|
|
2
2
|
//= require underscore.string
|
3
3
|
(function(_, Backbone) {
|
4
4
|
'use strict';
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
5
|
+
var global = this, fn = {};
|
6
|
+
function mixin(namespace, globalOptions) {
|
7
|
+
namespace = namespace || global || {};
|
8
|
+
globalOptions = globalOptions || {};
|
9
|
+
_(fn).extend({
|
10
|
+
mergeOptions: function() {
|
11
|
+
return _(arguments).chain().toArray().reduce(function(result, options) { return _(result).extend(options); }, {})
|
12
|
+
.omit('class', 'className', 'inverseOf', 'parseName', 'through').value();
|
13
|
+
},
|
14
|
+
|
15
|
+
buildAssociation: function(associationType, associationName, options) {
|
16
|
+
function through() {
|
17
|
+
function association() {
|
18
|
+
var t = (_(options.through).isFunction() && options.through.call(this)) || _.str.camelize(options.through);
|
19
|
+
return this[t] && this[t]() && this[t]()[associationName] && this[t]()[associationName]();
|
20
|
+
}
|
21
|
+
return options.through && association.call(this);
|
18
22
|
}
|
19
|
-
return options.through && association.call(this);
|
20
|
-
}
|
21
|
-
|
22
|
-
function throughCollection() {
|
23
|
-
return (this.collection && this.collection[associationName] && this.collection[associationName]()) ||
|
24
|
-
(this._options && this._options.collection && this._options.collection[associationName] && this._options.collection[associationName]());
|
25
|
-
}
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
if (options.inverseOf) {
|
32
|
-
newOptions[_.str.camelize(options.inverseOf)] = function() { return self; };
|
24
|
+
function throughCollection() {
|
25
|
+
return (this.collection && this.collection[associationName] && this.collection[associationName]()) ||
|
26
|
+
(this._options && this._options.collection && this._options.collection[associationName] && this._options.collection[associationName]());
|
33
27
|
}
|
34
28
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
through.call(self, association);
|
39
|
-
});
|
40
|
-
}
|
29
|
+
function createAssociation() {
|
30
|
+
var self = this, collectionName = _.str.classify(associationName), className = options.className && _.str.classify(options.className),
|
31
|
+
newOptions = fn.mergeOptions(options, globalOptions, this._options);
|
41
32
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
belongsTo: function() { return throughCollection.call(this) || through.call(this); }
|
46
|
-
};
|
47
|
-
|
48
|
-
this.prototype[associationName] = function() {
|
49
|
-
return (this._associations || (this._associations = {})) && this._associations[associationName] ||
|
50
|
-
(this._associations[associationName] = (this._options && _(this._options).result(associationName)) || associations[associationType].call(this));
|
51
|
-
};
|
52
|
-
}
|
53
|
-
|
54
|
-
function parseAssociation(associationType, associationName, options) {
|
55
|
-
function parseResponseWith(key, response) {
|
56
|
-
var result;
|
57
|
-
return _([_.str.camelize, _.str.underscored]).any(function(fn) {
|
58
|
-
var k = fn(key);
|
59
|
-
return (result = response[k] && {key: k, response: response[k]});
|
60
|
-
}) && result || {response: null};
|
61
|
-
}
|
33
|
+
if (options.inverseOf) {
|
34
|
+
newOptions[_.str.camelize(options.inverseOf)] = function() { return self; };
|
35
|
+
}
|
62
36
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
37
|
+
return _((options['class'] && new options['class'](null, newOptions)) ||
|
38
|
+
(className && namespace[className] && new namespace[className](null, newOptions)) ||
|
39
|
+
(namespace[collectionName] && new namespace[collectionName](null, newOptions))).tap(function(association) {
|
40
|
+
through.call(self, association);
|
41
|
+
});
|
42
|
+
}
|
69
43
|
|
70
|
-
if (options.parse) {
|
71
44
|
var associations = {
|
72
|
-
hasMany:
|
73
|
-
|
74
|
-
}
|
75
|
-
hasOne: function(assocResponse, association, newOptions) {
|
76
|
-
association.clear({silent: true}).set(assocResponse, newOptions);
|
77
|
-
}
|
45
|
+
hasMany: createAssociation,
|
46
|
+
hasOne: function() { return throughCollection.call(this) || createAssociation.call(this); },
|
47
|
+
belongsTo: function() { return throughCollection.call(this) || through.call(this); }
|
78
48
|
};
|
79
49
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
50
|
+
this.prototype[associationName] = function() {
|
51
|
+
return (this._associations || (this._associations = {})) && this._associations[associationName] ||
|
52
|
+
(this._associations[associationName] = (this._options && _(this._options).result(associationName)) || associations[associationType].call(this));
|
53
|
+
};
|
54
|
+
},
|
55
|
+
|
56
|
+
parseAssociation: function(associationType, associationName, options) {
|
57
|
+
function parseResponseWith(key, response) {
|
58
|
+
var result;
|
59
|
+
return _([_.str.camelize, _.str.underscored]).any(function(fn) {
|
60
|
+
var k = fn(key);
|
61
|
+
return (result = response[k] && {key: k, response: response[k]});
|
62
|
+
}) && result || {response: null};
|
63
|
+
}
|
64
|
+
|
65
|
+
function through(response) {
|
66
|
+
var t = parseResponseWith(_(options).result('through'), response).response,
|
67
|
+
singularAssociationName = _.singularize && _(associationName).singularize(),
|
68
|
+
p = options.parseName || singularAssociationName;
|
69
|
+
return {response: t && p && _(t)[associationType === 'hasOne' ? 'result' : 'pluck'](p)};
|
70
|
+
}
|
99
71
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
function(response) {
|
105
|
-
return (options.through && through.call(this, response)) ||
|
106
|
-
(options.parseName && parseResponseWith(options.parseName, response)) ||
|
107
|
-
(options.className && parseResponseWith(options.className, response)) ||
|
108
|
-
parseResponseWith(associationName, response);
|
109
|
-
};
|
72
|
+
if (options.parse) {
|
73
|
+
var associations = {
|
74
|
+
hasMany: function(assocResponse, association, newOptions) {
|
75
|
+
association.add(assocResponse, newOptions);
|
110
76
|
},
|
111
|
-
|
112
|
-
|
113
|
-
associations[associationType].call(this, assocResponse, this[associationName](), mergeAssociationOptions(options, this._options));
|
77
|
+
hasOne: function(assocResponse, association, newOptions) {
|
78
|
+
association.clear({silent: true}).set(assocResponse, newOptions);
|
114
79
|
}
|
115
|
-
}
|
80
|
+
};
|
81
|
+
|
82
|
+
if (associations[associationType]) {
|
83
|
+
var parsers = this._parsers;
|
84
|
+
if (_(parsers).isEmpty()) {
|
85
|
+
this.prototype.parse = _(this.prototype.parse).wrap(function(oldParse, response) {
|
86
|
+
var self = this;
|
87
|
+
return _(oldParse.call(self, response)).tap(function(parsedResponse) {
|
88
|
+
_(parsers)
|
89
|
+
.chain()
|
90
|
+
.map(function(parser) {
|
91
|
+
return _(parser.parseFn.call(self).call(self, parsedResponse)).tap(function(result) {
|
92
|
+
parser.associationFn.call(self, result.response);
|
93
|
+
}).key;
|
94
|
+
})
|
95
|
+
.each(function(key) {
|
96
|
+
return key && delete parsedResponse[key];
|
97
|
+
});
|
98
|
+
});
|
99
|
+
});
|
100
|
+
}
|
101
|
+
|
102
|
+
this._parsers.push({
|
103
|
+
parseFn: function() {
|
104
|
+
return _(options.parse).isFunction() &&
|
105
|
+
function(response) { return {response: options.parse.call(this, response) }; } ||
|
106
|
+
function(response) {
|
107
|
+
return (options.through && through.call(this, response)) ||
|
108
|
+
(options.parseName && parseResponseWith(options.parseName, response)) ||
|
109
|
+
(options.className && parseResponseWith(options.className, response)) ||
|
110
|
+
parseResponseWith(associationName, response);
|
111
|
+
};
|
112
|
+
},
|
113
|
+
associationFn: function(assocResponse) {
|
114
|
+
return assocResponse &&
|
115
|
+
associations[associationType].call(this, assocResponse, this[associationName](), fn.mergeOptions(options, globalOptions, this._options));
|
116
|
+
}
|
117
|
+
});
|
118
|
+
}
|
116
119
|
}
|
117
120
|
}
|
118
|
-
}
|
121
|
+
});
|
119
122
|
|
120
123
|
return {
|
121
124
|
included: function(source) {
|
@@ -128,8 +131,8 @@
|
|
128
131
|
if (options.parse && !_(this).has('_parsers')) {
|
129
132
|
this._parsers = [];
|
130
133
|
}
|
131
|
-
buildAssociation.call(this, associationType, associationName, options);
|
132
|
-
parseAssociation.call(this, associationType, associationName, options);
|
134
|
+
fn.buildAssociation.call(this, associationType, associationName, options);
|
135
|
+
fn.parseAssociation.call(this, associationType, associationName, options);
|
133
136
|
return this;
|
134
137
|
};
|
135
138
|
return associations;
|
@@ -164,6 +167,7 @@
|
|
164
167
|
}
|
165
168
|
};
|
166
169
|
}
|
170
|
+
mixin.fn = fn;
|
167
171
|
|
168
172
|
Backbone.extensions = _(Backbone.extensions || {}).extend({associations: mixin});
|
169
173
|
}).call(this, _, Backbone);
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: backbone_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-01-
|
13
|
+
date: 2013-01-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: fuubar
|
@@ -126,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
segments:
|
128
128
|
- 0
|
129
|
-
hash:
|
129
|
+
hash: 1206709655256402287
|
130
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
131
|
none: false
|
132
132
|
requirements:
|
@@ -135,10 +135,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
segments:
|
137
137
|
- 0
|
138
|
-
hash:
|
138
|
+
hash: 1206709655256402287
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 1.8.
|
141
|
+
rubygems_version: 1.8.24
|
142
142
|
signing_key:
|
143
143
|
specification_version: 3
|
144
144
|
summary: Extensions to backbone javascript library as a rails engine
|