Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
todomvc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Sven Franck
todomvc
Commits
d501a8f5
Commit
d501a8f5
authored
Mar 10, 2013
by
Pascal Hartig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backbone + RequireJS: Use Bower components
Ref #475
parent
037d063a
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
11270 additions
and
350 deletions
+11270
-350
dependency-examples/backbone_require/component.json
dependency-examples/backbone_require/component.json
+13
-0
dependency-examples/backbone_require/components/backbone.localStorage/backbone.localStorage.js
...components/backbone.localStorage/backbone.localStorage.js
+88
-26
dependency-examples/backbone_require/components/backbone/backbone.js
...examples/backbone_require/components/backbone/backbone.js
+234
-269
dependency-examples/backbone_require/components/jquery/jquery.js
...ncy-examples/backbone_require/components/jquery/jquery.js
+9597
-0
dependency-examples/backbone_require/components/requirejs-text/text.js
...amples/backbone_require/components/requirejs-text/text.js
+38
-14
dependency-examples/backbone_require/components/requirejs/require.js
...examples/backbone_require/components/requirejs/require.js
+61
-35
dependency-examples/backbone_require/components/todomvc-common/base.js
...amples/backbone_require/components/todomvc-common/base.js
+7
-0
dependency-examples/backbone_require/components/underscore/underscore.js
...ples/backbone_require/components/underscore/underscore.js
+1226
-0
dependency-examples/backbone_require/index.html
dependency-examples/backbone_require/index.html
+1
-1
dependency-examples/backbone_require/js/main.js
dependency-examples/backbone_require/js/main.js
+5
-5
No files found.
dependency-examples/backbone_require/component.json
0 → 100644
View file @
d501a8f5
{
"name"
:
"todomvc-backbone-requirejs"
,
"version"
:
"0.0.0"
,
"dependencies"
:
{
"backbone"
:
"~0.9.10"
,
"underscore"
:
"~1.4.4"
,
"jquery"
:
"~1.9.1"
,
"todomvc-common"
:
"~0.1.0"
,
"backbone.localStorage"
:
"~1.1.0"
,
"requirejs"
:
"~2.1.5"
,
"requirejs-text"
:
"~2.0.5"
}
}
dependency-examples/backbone_require/
js/lib/backbon
e/backbone.localStorage.js
→
dependency-examples/backbone_require/
components/backbone.localStorag
e/backbone.localStorage.js
View file @
d501a8f5
/**
* Backbone localStorage Adapter
* Version 1.1.0
*
* https://github.com/jeromegn/Backbone.localStorage
*/
(
function
(
_
,
Backbone
)
{
(
function
(
root
,
factory
)
{
if
(
typeof
define
===
"
function
"
&&
define
.
amd
)
{
// AMD. Register as an anonymous module.
define
([
"
underscore
"
,
"
backbone
"
],
function
(
_
,
Backbone
)
{
// Use global variables if the locals is undefined.
return
factory
(
_
||
root
.
_
,
Backbone
||
root
.
Backbone
);
});
}
else
{
// RequireJS isn't being used. Assume underscore and backbone is loaded in <script> tags
factory
(
_
,
Backbone
);
}
}(
this
,
function
(
_
,
Backbone
)
{
// A simple module to replace `Backbone.sync` with *localStorage*-based
// persistence. Models are given GUIDS, and saved into a JSON object. Simple
// as that.
...
...
@@ -47,39 +59,51 @@ _.extend(Backbone.LocalStorage.prototype, {
this
.
localStorage
().
setItem
(
this
.
name
+
"
-
"
+
model
.
id
,
JSON
.
stringify
(
model
));
this
.
records
.
push
(
model
.
id
.
toString
());
this
.
save
();
return
model
.
toJSON
(
);
return
this
.
find
(
model
);
},
// Update a model by replacing its copy in `this.data`.
update
:
function
(
model
)
{
this
.
localStorage
().
setItem
(
this
.
name
+
"
-
"
+
model
.
id
,
JSON
.
stringify
(
model
));
if
(
!
_
.
include
(
this
.
records
,
model
.
id
.
toString
()))
this
.
records
.
push
(
model
.
id
.
toString
());
this
.
save
();
return
model
.
toJSON
();
if
(
!
_
.
include
(
this
.
records
,
model
.
id
.
toString
()))
this
.
records
.
push
(
model
.
id
.
toString
());
this
.
save
();
return
this
.
find
(
model
);
},
// Retrieve a model from `this.data` by id.
find
:
function
(
model
)
{
return
JSON
.
parse
(
this
.
localStorage
().
getItem
(
this
.
name
+
"
-
"
+
model
.
id
));
return
this
.
jsonData
(
this
.
localStorage
().
getItem
(
this
.
name
+
"
-
"
+
model
.
id
));
},
// Return the array of all models currently in storage.
findAll
:
function
()
{
return
_
(
this
.
records
).
chain
()
.
map
(
function
(
id
){
return
JSON
.
parse
(
this
.
localStorage
().
getItem
(
this
.
name
+
"
-
"
+
id
));},
this
)
.
map
(
function
(
id
){
return
this
.
jsonData
(
this
.
localStorage
().
getItem
(
this
.
name
+
"
-
"
+
id
));
},
this
)
.
compact
()
.
value
();
},
// Delete a model from `this.data`, returning it.
destroy
:
function
(
model
)
{
if
(
model
.
isNew
())
return
false
this
.
localStorage
().
removeItem
(
this
.
name
+
"
-
"
+
model
.
id
);
this
.
records
=
_
.
reject
(
this
.
records
,
function
(
record_id
){
return
record_id
==
model
.
id
.
toString
();});
this
.
records
=
_
.
reject
(
this
.
records
,
function
(
id
){
return
id
===
model
.
id
.
toString
();
});
this
.
save
();
return
model
;
},
localStorage
:
function
()
{
return
localStorage
;
},
// fix for "illegal access" error on Android when JSON.parse is passed null
jsonData
:
function
(
data
)
{
return
data
&&
JSON
.
parse
(
data
);
}
});
...
...
@@ -90,22 +114,60 @@ _.extend(Backbone.LocalStorage.prototype, {
Backbone
.
LocalStorage
.
sync
=
window
.
Store
.
sync
=
Backbone
.
localSync
=
function
(
method
,
model
,
options
)
{
var
store
=
model
.
localStorage
||
model
.
collection
.
localStorage
;
var
resp
,
syncDfd
=
$
.
Deferred
&&
$
.
Deferred
();
//If $ is having Deferred - use it.
var
resp
,
errorMessage
,
syncDfd
=
$
.
Deferred
&&
$
.
Deferred
();
//If $ is having Deferred - use it.
try
{
switch
(
method
)
{
case
"
read
"
:
resp
=
model
.
id
!=
undefined
?
store
.
find
(
model
)
:
store
.
findAll
();
break
;
case
"
create
"
:
resp
=
store
.
create
(
model
);
break
;
case
"
update
"
:
resp
=
store
.
update
(
model
);
break
;
case
"
delete
"
:
resp
=
store
.
destroy
(
model
);
break
;
case
"
read
"
:
resp
=
model
.
id
!=
undefined
?
store
.
find
(
model
)
:
store
.
findAll
();
break
;
case
"
create
"
:
resp
=
store
.
create
(
model
);
break
;
case
"
update
"
:
resp
=
store
.
update
(
model
);
break
;
case
"
delete
"
:
resp
=
store
.
destroy
(
model
);
break
;
}
}
catch
(
error
)
{
if
(
error
.
code
===
DOMException
.
QUOTA_EXCEEDED_ERR
&&
window
.
localStorage
.
length
===
0
)
errorMessage
=
"
Private browsing is unsupported
"
;
else
errorMessage
=
error
.
message
;
}
if
(
resp
)
{
if
(
options
&&
options
.
success
)
options
.
success
(
resp
);
if
(
syncDfd
)
syncDfd
.
resolve
();
if
(
options
&&
options
.
success
)
if
(
Backbone
.
VERSION
===
"
0.9.10
"
)
{
options
.
success
(
model
,
resp
,
options
);
}
else
{
if
(
options
&&
options
.
error
)
options
.
error
(
"
Record not found
"
);
if
(
syncDfd
)
syncDfd
.
reject
();
options
.
success
(
resp
);
}
if
(
syncDfd
)
syncDfd
.
resolve
(
resp
);
}
else
{
errorMessage
=
errorMessage
?
errorMessage
:
"
Record Not Found
"
;
if
(
options
&&
options
.
error
)
if
(
Backbone
.
VERSION
===
"
0.9.10
"
)
{
options
.
error
(
model
,
errorMessage
,
options
);
}
else
{
options
.
error
(
errorMessage
);
}
if
(
syncDfd
)
syncDfd
.
reject
(
errorMessage
);
}
// add compatibility with $.ajax
// always execute callback for success and error
if
(
options
&&
options
.
complete
)
options
.
complete
(
resp
);
return
syncDfd
&&
syncDfd
.
promise
();
};
...
...
@@ -113,8 +175,7 @@ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(m
Backbone
.
ajaxSync
=
Backbone
.
sync
;
Backbone
.
getSyncMethod
=
function
(
model
)
{
if
(
model
.
localStorage
||
(
model
.
collection
&&
model
.
collection
.
localStorage
))
{
if
(
model
.
localStorage
||
(
model
.
collection
&&
model
.
collection
.
localStorage
))
{
return
Backbone
.
localSync
;
}
...
...
@@ -127,4 +188,5 @@ Backbone.sync = function(method, model, options) {
return
Backbone
.
getSyncMethod
(
model
).
apply
(
this
,
[
method
,
model
,
options
]);
};
})(
_
,
Backbone
);
return
Backbone
.
LocalStorage
;
}));
\ No newline at end of file
dependency-examples/backbone_require/
js/lib
/backbone/backbone.js
→
dependency-examples/backbone_require/
components
/backbone/backbone.js
View file @
d501a8f5
// Backbone.js 0.9.
9
// Backbone.js 0.9.
10
// (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
...
...
@@ -34,7 +34,7 @@
}
// Current version of the library. Keep in sync with `package.json`.
Backbone
.
VERSION
=
'
0.9.
9
'
;
Backbone
.
VERSION
=
'
0.9.
10
'
;
// Require Underscore, if we're on the server, and it's not already present.
var
_
=
root
.
_
;
...
...
@@ -88,7 +88,7 @@
// Optimized internal dispatch function for triggering events. Tries to
// keep the usual cases speedy (most Backbone events have 3 arguments).
var
triggerEvents
=
function
(
obj
,
events
,
args
)
{
var
triggerEvents
=
function
(
events
,
args
)
{
var
ev
,
i
=
-
1
,
l
=
events
.
length
;
switch
(
args
.
length
)
{
case
0
:
while
(
++
i
<
l
)
(
ev
=
events
[
i
]).
callback
.
call
(
ev
.
ctx
);
...
...
@@ -142,7 +142,7 @@
// Remove one or many callbacks. If `context` is null, removes all
// callbacks with that function. If `callback` is null, removes all
// callbacks for the event. If `
events
` is null, removes all bound
// callbacks for the event. If `
name
` is null, removes all bound
// callbacks for all events.
off
:
function
(
name
,
callback
,
context
)
{
var
list
,
ev
,
events
,
names
,
i
,
l
,
j
,
k
;
...
...
@@ -160,7 +160,8 @@
if
(
callback
||
context
)
{
for
(
j
=
0
,
k
=
list
.
length
;
j
<
k
;
j
++
)
{
ev
=
list
[
j
];
if
((
callback
&&
callback
!==
(
ev
.
callback
.
_callback
||
ev
.
callback
))
||
if
((
callback
&&
callback
!==
ev
.
callback
&&
callback
!==
ev
.
callback
.
_callback
)
||
(
context
&&
context
!==
ev
.
context
))
{
events
.
push
(
ev
);
}
...
...
@@ -183,32 +184,33 @@
if
(
!
eventsApi
(
this
,
'
trigger
'
,
name
,
args
))
return
this
;
var
events
=
this
.
_events
[
name
];
var
allEvents
=
this
.
_events
.
all
;
if
(
events
)
triggerEvents
(
this
,
events
,
args
);
if
(
allEvents
)
triggerEvents
(
this
,
allEvents
,
arguments
);
if
(
events
)
triggerEvents
(
events
,
args
);
if
(
allEvents
)
triggerEvents
(
allEvents
,
arguments
);
return
this
;
},
// An inversion-of-control version of `on`. Tell *this* object to listen to
// an event in another object ... keeping track of what it's listening to.
listenTo
:
function
(
obj
ect
,
events
,
callback
)
{
listenTo
:
function
(
obj
,
name
,
callback
)
{
var
listeners
=
this
.
_listeners
||
(
this
.
_listeners
=
{});
var
id
=
obj
ect
.
_listenerId
||
(
object
.
_listenerId
=
_
.
uniqueId
(
'
l
'
));
listeners
[
id
]
=
obj
ect
;
obj
ect
.
on
(
events
,
callback
||
this
,
this
);
var
id
=
obj
.
_listenerId
||
(
obj
.
_listenerId
=
_
.
uniqueId
(
'
l
'
));
listeners
[
id
]
=
obj
;
obj
.
on
(
name
,
typeof
name
===
'
object
'
?
this
:
callback
,
this
);
return
this
;
},
// Tell this object to stop listening to either specific events ... or
// to every object it's currently listening to.
stopListening
:
function
(
obj
ect
,
events
,
callback
)
{
stopListening
:
function
(
obj
,
name
,
callback
)
{
var
listeners
=
this
.
_listeners
;
if
(
!
listeners
)
return
;
if
(
obj
ect
)
{
obj
ect
.
off
(
events
,
callback
,
this
);
if
(
!
events
&&
!
callback
)
delete
listeners
[
object
.
_listenerId
];
if
(
obj
)
{
obj
.
off
(
name
,
typeof
name
===
'
object
'
?
this
:
callback
,
this
);
if
(
!
name
&&
!
callback
)
delete
listeners
[
obj
.
_listenerId
];
}
else
{
if
(
typeof
name
===
'
object
'
)
callback
=
this
;
for
(
var
id
in
listeners
)
{
listeners
[
id
].
off
(
n
ull
,
null
,
this
);
listeners
[
id
].
off
(
n
ame
,
callback
,
this
);
}
this
.
_listeners
=
{};
}
...
...
@@ -233,15 +235,14 @@
var
defaults
;
var
attrs
=
attributes
||
{};
this
.
cid
=
_
.
uniqueId
(
'
c
'
);
this
.
changed
=
{};
this
.
attributes
=
{};
this
.
_changes
=
[];
if
(
options
&&
options
.
collection
)
this
.
collection
=
options
.
collection
;
if
(
options
&&
options
.
parse
)
attrs
=
this
.
parse
(
attrs
);
if
(
defaults
=
_
.
result
(
this
,
'
defaults
'
))
_
.
defaults
(
attrs
,
defaults
);
this
.
set
(
attrs
,
{
silent
:
true
});
this
.
_currentAttributes
=
_
.
clone
(
this
.
attributes
);
this
.
_previousAttributes
=
_
.
clone
(
this
.
attributes
);
if
(
options
&&
options
.
parse
)
attrs
=
this
.
parse
(
attrs
,
options
)
||
{};
if
(
defaults
=
_
.
result
(
this
,
'
defaults
'
))
{
attrs
=
_
.
defaults
({},
attrs
,
defaults
);
}
this
.
set
(
attrs
,
options
);
this
.
changed
=
{};
this
.
initialize
.
apply
(
this
,
arguments
);
};
...
...
@@ -285,47 +286,72 @@
return
this
.
get
(
attr
)
!=
null
;
},
// ----------------------------------------------------------------------
// Set a hash of model attributes on the object, firing `"change"` unless
// you choose to silence it.
set
:
function
(
key
,
val
,
options
)
{
var
attr
,
attrs
;
var
attr
,
attrs
,
unset
,
changes
,
silent
,
changing
,
prev
,
current
;
if
(
key
==
null
)
return
this
;
// Handle both `"key", value` and `{key: value}` -style arguments.
if
(
_
.
isObject
(
key
)
)
{
if
(
typeof
key
===
'
object
'
)
{
attrs
=
key
;
options
=
val
;
}
else
{
(
attrs
=
{})[
key
]
=
val
;
}
// Extract attributes and options.
var
silent
=
options
&&
options
.
silent
;
var
unset
=
options
&&
options
.
unset
;
options
||
(
options
=
{});
// Run validation.
if
(
!
this
.
_validate
(
attrs
,
options
))
return
false
;
// Extract attributes and options.
unset
=
options
.
unset
;
silent
=
options
.
silent
;
changes
=
[];
changing
=
this
.
_changing
;
this
.
_changing
=
true
;
if
(
!
changing
)
{
this
.
_previousAttributes
=
_
.
clone
(
this
.
attributes
);
this
.
changed
=
{};
}
current
=
this
.
attributes
,
prev
=
this
.
_previousAttributes
;
// Check for changes of `id`.
if
(
this
.
idAttribute
in
attrs
)
this
.
id
=
attrs
[
this
.
idAttribute
];
var
now
=
this
.
attributes
;
// For each `set` attribute...
// For each `set` attribute, update or delete the current value.
for
(
attr
in
attrs
)
{
val
=
attrs
[
attr
];
// Update or delete the current value, and track the change.
unset
?
delete
now
[
attr
]
:
now
[
attr
]
=
val
;
this
.
_changes
.
push
(
attr
,
val
);
if
(
!
_
.
isEqual
(
current
[
attr
],
val
))
changes
.
push
(
attr
);
if
(
!
_
.
isEqual
(
prev
[
attr
],
val
))
{
this
.
changed
[
attr
]
=
val
;
}
else
{
delete
this
.
changed
[
attr
];
}
unset
?
delete
current
[
attr
]
:
current
[
attr
]
=
val
;
}
// Signal that the model's state has potentially changed, and we need
// to recompute the actual changes.
this
.
_hasComputed
=
false
;
// Trigger all relevant attribute changes.
if
(
!
silent
)
{
if
(
changes
.
length
)
this
.
_pending
=
true
;
for
(
var
i
=
0
,
l
=
changes
.
length
;
i
<
l
;
i
++
)
{
this
.
trigger
(
'
change:
'
+
changes
[
i
],
this
,
current
[
changes
[
i
]],
options
);
}
}
// Fire the `"change"` events.
if
(
!
silent
)
this
.
change
(
options
);
if
(
changing
)
return
this
;
if
(
!
silent
)
{
while
(
this
.
_pending
)
{
this
.
_pending
=
false
;
this
.
trigger
(
'
change
'
,
this
,
options
);
}
}
this
.
_pending
=
false
;
this
.
_changing
=
false
;
return
this
;
},
...
...
@@ -343,16 +369,54 @@
return
this
.
set
(
attrs
,
_
.
extend
({},
options
,
{
unset
:
true
}));
},
// Determine if the model has changed since the last `"change"` event.
// If you specify an attribute name, determine if that attribute has changed.
hasChanged
:
function
(
attr
)
{
if
(
attr
==
null
)
return
!
_
.
isEmpty
(
this
.
changed
);
return
_
.
has
(
this
.
changed
,
attr
);
},
// Return an object containing all the attributes that have changed, or
// false if there are no changed attributes. Useful for determining what
// parts of a view need to be updated and/or what attributes need to be
// persisted to the server. Unset attributes will be set to undefined.
// You can also pass an attributes object to diff against the model,
// determining if there *would be* a change.
changedAttributes
:
function
(
diff
)
{
if
(
!
diff
)
return
this
.
hasChanged
()
?
_
.
clone
(
this
.
changed
)
:
false
;
var
val
,
changed
=
false
;
var
old
=
this
.
_changing
?
this
.
_previousAttributes
:
this
.
attributes
;
for
(
var
attr
in
diff
)
{
if
(
_
.
isEqual
(
old
[
attr
],
(
val
=
diff
[
attr
])))
continue
;
(
changed
||
(
changed
=
{}))[
attr
]
=
val
;
}
return
changed
;
},
// Get the previous value of an attribute, recorded at the time the last
// `"change"` event was fired.
previous
:
function
(
attr
)
{
if
(
attr
==
null
||
!
this
.
_previousAttributes
)
return
null
;
return
this
.
_previousAttributes
[
attr
];
},
// Get all of the attributes of the model at the time of the previous
// `"change"` event.
previousAttributes
:
function
()
{
return
_
.
clone
(
this
.
_previousAttributes
);
},
// ---------------------------------------------------------------------
// Fetch the model from the server. If the server's representation of the
// model differs from its current attributes, they will be overriden,
// triggering a `"change"` event.
fetch
:
function
(
options
)
{
options
=
options
?
_
.
clone
(
options
)
:
{};
if
(
options
.
parse
===
void
0
)
options
.
parse
=
true
;
var
model
=
this
;
var
success
=
options
.
success
;
options
.
success
=
function
(
resp
,
status
,
xhr
)
{
if
(
!
model
.
set
(
model
.
parse
(
resp
),
options
))
return
false
;
options
.
success
=
function
(
model
,
resp
,
options
)
{
if
(
!
model
.
set
(
model
.
parse
(
resp
,
options
),
options
))
return
false
;
if
(
success
)
success
(
model
,
resp
,
options
);
};
return
this
.
sync
(
'
read
'
,
this
,
options
);
...
...
@@ -362,55 +426,51 @@
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save
:
function
(
key
,
val
,
options
)
{
var
attrs
,
current
,
done
;
var
attrs
,
success
,
method
,
xhr
,
attributes
=
this
.
attributes
;
// Handle both `"key", value` and `{key: value}` -style arguments.
if
(
key
==
null
||
_
.
isObject
(
key
)
)
{
if
(
key
==
null
||
typeof
key
===
'
object
'
)
{
attrs
=
key
;
options
=
val
;
}
else
if
(
key
!=
null
)
{
}
else
{
(
attrs
=
{})[
key
]
=
val
;
}
options
=
options
?
_
.
clone
(
options
)
:
{};
// If we're "wait"-ing to set changed attributes, validate early.
if
(
options
.
wait
)
{
if
(
attrs
&&
!
this
.
_validate
(
attrs
,
options
))
return
false
;
current
=
_
.
clone
(
this
.
attributes
);
}
// If we're not waiting and attributes exist, save acts as `set(attr).save(null, opts)`.
if
(
attrs
&&
(
!
options
||
!
options
.
wait
)
&&
!
this
.
set
(
attrs
,
options
))
return
false
;
// Regular saves `set` attributes before persisting to the server.
var
silentOptions
=
_
.
extend
({},
options
,
{
silent
:
true
});
if
(
attrs
&&
!
this
.
set
(
attrs
,
options
.
wait
?
silentOptions
:
options
))
{
return
false
;
}
options
=
_
.
extend
({
validate
:
true
},
options
);
// Do not persist invalid models.
if
(
!
attrs
&&
!
this
.
_validate
(
null
,
options
))
return
false
;
if
(
!
this
.
_validate
(
attrs
,
options
))
return
false
;
// Set temporary attributes if `{wait: true}`.
if
(
attrs
&&
options
.
wait
)
{
this
.
attributes
=
_
.
extend
({},
attributes
,
attrs
);
}
// After a successful server-side save, the client is (optionally)
// updated with the server-side state.
var
model
=
this
;
var
success
=
options
.
success
;
options
.
success
=
function
(
resp
,
status
,
xhr
)
{
done
=
true
;
var
serverAttrs
=
model
.
parse
(
resp
);
if
(
options
.
parse
===
void
0
)
options
.
parse
=
true
;
success
=
options
.
success
;
options
.
success
=
function
(
model
,
resp
,
options
)
{
// Ensure attributes are restored during synchronous saves.
model
.
attributes
=
attributes
;
var
serverAttrs
=
model
.
parse
(
resp
,
options
);
if
(
options
.
wait
)
serverAttrs
=
_
.
extend
(
attrs
||
{},
serverAttrs
);
if
(
!
model
.
set
(
serverAttrs
,
options
))
return
false
;
if
(
_
.
isObject
(
serverAttrs
)
&&
!
model
.
set
(
serverAttrs
,
options
))
{
return
false
;
}
if
(
success
)
success
(
model
,
resp
,
options
);
};
// Finish configuring and sending the Ajax request.
var
method
=
this
.
isNew
()
?
'
create
'
:
(
options
.
patch
?
'
patch
'
:
'
update
'
);
if
(
method
==
'
patch
'
)
options
.
attrs
=
attrs
;
var
xhr
=
this
.
sync
(
method
,
this
,
options
);
method
=
this
.
isNew
()
?
'
create
'
:
(
options
.
patch
?
'
patch
'
:
'
update
'
);
if
(
method
==
=
'
patch
'
)
options
.
attrs
=
attrs
;
xhr
=
this
.
sync
(
method
,
this
,
options
);
// When using `wait`, reset attributes to original values unless
// `success` has been called already.
if
(
!
done
&&
options
.
wait
)
{
this
.
clear
(
silentOptions
);
this
.
set
(
current
,
silentOptions
);
}
// Restore attributes.
if
(
attrs
&&
options
.
wait
)
this
.
attributes
=
attributes
;
return
xhr
;
},
...
...
@@ -427,13 +487,13 @@
model
.
trigger
(
'
destroy
'
,
model
,
model
.
collection
,
options
);
};
options
.
success
=
function
(
resp
)
{
options
.
success
=
function
(
model
,
resp
,
options
)
{
if
(
options
.
wait
||
model
.
isNew
())
destroy
();
if
(
success
)
success
(
model
,
resp
,
options
);
};
if
(
this
.
isNew
())
{
options
.
success
();
options
.
success
(
this
,
null
,
options
);
return
false
;
}
...
...
@@ -453,7 +513,7 @@
// **parse** converts a response into the hash of attributes to be `set` on
// the model. The default implementation is just to pass the response along.
parse
:
function
(
resp
)
{
parse
:
function
(
resp
,
options
)
{
return
resp
;
},
...
...
@@ -467,115 +527,20 @@
return
this
.
id
==
null
;
},
// Call this method to manually fire a `"change"` event for this model and
// a `"change:attribute"` event for each changed attribute.
// Calling this will cause all objects observing the model to update.
change
:
function
(
options
)
{
var
changing
=
this
.
_changing
;
this
.
_changing
=
true
;
// Generate the changes to be triggered on the model.
var
triggers
=
this
.
_computeChanges
(
true
);
this
.
_pending
=
!!
triggers
.
length
;
for
(
var
i
=
triggers
.
length
-
2
;
i
>=
0
;
i
-=
2
)
{
this
.
trigger
(
'
change:
'
+
triggers
[
i
],
this
,
triggers
[
i
+
1
],
options
);
}
if
(
changing
)
return
this
;
// Trigger a `change` while there have been changes.
while
(
this
.
_pending
)
{
this
.
_pending
=
false
;
this
.
trigger
(
'
change
'
,
this
,
options
);
this
.
_previousAttributes
=
_
.
clone
(
this
.
attributes
);
}
this
.
_changing
=
false
;
return
this
;
},
// Determine if the model has changed since the last `"change"` event.
// If you specify an attribute name, determine if that attribute has changed.
hasChanged
:
function
(
attr
)
{
if
(
!
this
.
_hasComputed
)
this
.
_computeChanges
();
if
(
attr
==
null
)
return
!
_
.
isEmpty
(
this
.
changed
);
return
_
.
has
(
this
.
changed
,
attr
);
},
// Return an object containing all the attributes that have changed, or
// false if there are no changed attributes. Useful for determining what
// parts of a view need to be updated and/or what attributes need to be
// persisted to the server. Unset attributes will be set to undefined.
// You can also pass an attributes object to diff against the model,
// determining if there *would be* a change.
changedAttributes
:
function
(
diff
)
{
if
(
!
diff
)
return
this
.
hasChanged
()
?
_
.
clone
(
this
.
changed
)
:
false
;
var
val
,
changed
=
false
,
old
=
this
.
_previousAttributes
;
for
(
var
attr
in
diff
)
{
if
(
_
.
isEqual
(
old
[
attr
],
(
val
=
diff
[
attr
])))
continue
;
(
changed
||
(
changed
=
{}))[
attr
]
=
val
;
}
return
changed
;
},
// Looking at the built up list of `set` attribute changes, compute how
// many of the attributes have actually changed. If `loud`, return a
// boiled-down list of only the real changes.
_computeChanges
:
function
(
loud
)
{
this
.
changed
=
{};
var
already
=
{};
var
triggers
=
[];
var
current
=
this
.
_currentAttributes
;
var
changes
=
this
.
_changes
;
// Loop through the current queue of potential model changes.
for
(
var
i
=
changes
.
length
-
2
;
i
>=
0
;
i
-=
2
)
{
var
key
=
changes
[
i
],
val
=
changes
[
i
+
1
];
if
(
already
[
key
])
continue
;
already
[
key
]
=
true
;
// Check if the attribute has been modified since the last change,
// and update `this.changed` accordingly. If we're inside of a `change`
// call, also add a trigger to the list.
if
(
current
[
key
]
!==
val
)
{
this
.
changed
[
key
]
=
val
;
if
(
!
loud
)
continue
;
triggers
.
push
(
key
,
val
);
current
[
key
]
=
val
;
}
}
if
(
loud
)
this
.
_changes
=
[];
// Signals `this.changed` is current to prevent duplicate calls from `this.hasChanged`.
this
.
_hasComputed
=
true
;
return
triggers
;
},
// Get the previous value of an attribute, recorded at the time the last
// `"change"` event was fired.
previous
:
function
(
attr
)
{
if
(
attr
==
null
||
!
this
.
_previousAttributes
)
return
null
;
return
this
.
_previousAttributes
[
attr
];
},
// Get all of the attributes of the model at the time of the previous
// `"change"` event.
previousAttributes
:
function
()
{
return
_
.
clone
(
this
.
_previousAttributes
);
// Check if the model is currently in a valid state.
isValid
:
function
(
options
)
{
return
!
this
.
validate
||
!
this
.
validate
(
this
.
attributes
,
options
);
},
// Run validation against the next complete set of model attributes,
// returning `true` if all is well.
If a specific `error` callback has
//
been passed, call that instead of firing the general `"error"` event
.
// returning `true` if all is well.
Otherwise, fire a general
//
`"error"` event and call the error callback, if specified
.
_validate
:
function
(
attrs
,
options
)
{
if
(
!
this
.
validate
)
return
true
;
if
(
!
options
.
validate
||
!
this
.
validate
)
return
true
;
attrs
=
_
.
extend
({},
this
.
attributes
,
attrs
);
var
error
=
this
.
validat
e
(
attrs
,
options
)
;
var
error
=
this
.
validat
ionError
=
this
.
validate
(
attrs
,
options
)
||
null
;
if
(
!
error
)
return
true
;
if
(
options
&&
options
.
error
)
options
.
error
(
this
,
error
,
options
);
this
.
trigger
(
'
error
'
,
this
,
error
,
options
);
this
.
trigger
(
'
invalid
'
,
this
,
error
,
options
||
{});
return
false
;
}
...
...
@@ -591,6 +556,7 @@
options
||
(
options
=
{});
if
(
options
.
model
)
this
.
model
=
options
.
model
;
if
(
options
.
comparator
!==
void
0
)
this
.
comparator
=
options
.
comparator
;
this
.
models
=
[];
this
.
_reset
();
this
.
initialize
.
apply
(
this
,
arguments
);
if
(
models
)
this
.
reset
(
models
,
_
.
extend
({
silent
:
true
},
options
));
...
...
@@ -618,74 +584,81 @@
return
Backbone
.
sync
.
apply
(
this
,
arguments
);
},
// Add a model, or list of models to the set. Pass **silent** to avoid
// firing the `add` event for every new model.
// Add a model, or list of models to the set.
add
:
function
(
models
,
options
)
{
var
i
,
args
,
length
,
model
,
existing
,
needsSort
;
var
at
=
options
&&
options
.
at
;
var
sort
=
((
options
&&
options
.
sort
)
==
null
?
true
:
options
.
sort
);
models
=
_
.
isArray
(
models
)
?
models
.
slice
()
:
[
models
];
options
||
(
options
=
{});
var
i
,
l
,
model
,
attrs
,
existing
,
doSort
,
add
,
at
,
sort
,
sortAttr
;
add
=
[];
at
=
options
.
at
;
sort
=
this
.
comparator
&&
(
at
==
null
)
&&
options
.
sort
!=
false
;
sortAttr
=
_
.
isString
(
this
.
comparator
)
?
this
.
comparator
:
null
;
// Turn bare objects into model references, and prevent invalid models
// from being added.
for
(
i
=
models
.
length
-
1
;
i
>=
0
;
i
--
)
{
if
(
!
(
model
=
this
.
_prepareModel
(
models
[
i
],
options
)))
{
this
.
trigger
(
"
error
"
,
this
,
models
[
i
],
options
);
models
.
splice
(
i
,
1
);
for
(
i
=
0
,
l
=
models
.
length
;
i
<
l
;
i
++
)
{
if
(
!
(
model
=
this
.
_prepareModel
(
attrs
=
models
[
i
],
options
)))
{
this
.
trigger
(
'
invalid
'
,
this
,
attrs
,
options
);
continue
;
}
models
[
i
]
=
model
;
existing
=
model
.
id
!=
null
&&
this
.
_byId
[
model
.
id
];
// If a duplicate is found, prevent it from being added and
// optionally merge it into the existing model.
if
(
existing
||
this
.
_byCid
[
model
.
cid
]
)
{
if
(
options
&&
options
.
merge
&&
existing
)
{
existing
.
set
(
model
.
attribute
s
,
options
);
needsSort
=
sort
;
if
(
existing
=
this
.
get
(
model
)
)
{
if
(
options
.
merge
)
{
existing
.
set
(
attrs
===
model
?
model
.
attributes
:
attr
s
,
options
);
if
(
sort
&&
!
doSort
&&
existing
.
hasChanged
(
sortAttr
))
doSort
=
true
;
}
models
.
splice
(
i
,
1
);
continue
;
}
// This is a new model, push it to the `add` list.
add
.
push
(
model
);
// Listen to added models' events, and index models for lookup by
// `id` and by `cid`.
model
.
on
(
'
all
'
,
this
.
_onModelEvent
,
this
);
this
.
_by
Ci
d
[
model
.
cid
]
=
model
;
this
.
_by
I
d
[
model
.
cid
]
=
model
;
if
(
model
.
id
!=
null
)
this
.
_byId
[
model
.
id
]
=
model
;
}
// See if sorting is needed, update `length` and splice in new models.
if
(
models
.
length
)
needsSort
=
sort
;
this
.
length
+=
models
.
length
;
args
=
[
at
!=
null
?
at
:
this
.
models
.
length
,
0
];
push
.
apply
(
args
,
models
);
splice
.
apply
(
this
.
models
,
args
);
if
(
add
.
length
)
{
if
(
sort
)
doSort
=
true
;
this
.
length
+=
add
.
length
;
if
(
at
!=
null
)
{
splice
.
apply
(
this
.
models
,
[
at
,
0
].
concat
(
add
));
}
else
{
push
.
apply
(
this
.
models
,
add
);
}
}
// Sort the collection if appropriate.
if
(
needsSort
&&
this
.
comparator
&&
at
==
null
)
this
.
sort
({
silent
:
true
});
// S
ilently s
ort the collection if appropriate.
if
(
doSort
)
this
.
sort
({
silent
:
true
});
if
(
options
&&
options
.
silent
)
return
this
;
if
(
options
.
silent
)
return
this
;
// Trigger `add` events.
while
(
model
=
models
.
shift
()
)
{
model
.
trigger
(
'
add
'
,
model
,
this
,
options
);
for
(
i
=
0
,
l
=
add
.
length
;
i
<
l
;
i
++
)
{
(
model
=
add
[
i
])
.
trigger
(
'
add
'
,
model
,
this
,
options
);
}
// Trigger `sort` if the collection was sorted.
if
(
doSort
)
this
.
trigger
(
'
sort
'
,
this
,
options
);
return
this
;
},
// Remove a model, or a list of models from the set. Pass silent to avoid
// firing the `remove` event for every model removed.
// Remove a model, or a list of models from the set.
remove
:
function
(
models
,
options
)
{
var
i
,
l
,
index
,
model
;
options
||
(
options
=
{});
models
=
_
.
isArray
(
models
)
?
models
.
slice
()
:
[
models
];
options
||
(
options
=
{});
var
i
,
l
,
index
,
model
;
for
(
i
=
0
,
l
=
models
.
length
;
i
<
l
;
i
++
)
{
model
=
this
.
get
(
models
[
i
]);
if
(
!
model
)
continue
;
delete
this
.
_byId
[
model
.
id
];
delete
this
.
_by
Ci
d
[
model
.
cid
];
delete
this
.
_by
I
d
[
model
.
cid
];
index
=
this
.
indexOf
(
model
);
this
.
models
.
splice
(
index
,
1
);
this
.
length
--
;
...
...
@@ -734,7 +707,8 @@
// Get a model from the set by id.
get
:
function
(
obj
)
{
if
(
obj
==
null
)
return
void
0
;
return
this
.
_byId
[
obj
.
id
!=
null
?
obj
.
id
:
obj
]
||
this
.
_byCid
[
obj
.
cid
||
obj
];
this
.
_idAttr
||
(
this
.
_idAttr
=
this
.
model
.
prototype
.
idAttribute
);
return
this
.
_byId
[
obj
.
id
||
obj
.
cid
||
obj
[
this
.
_idAttr
]
||
obj
];
},
// Get the model at the given index.
...
...
@@ -760,14 +734,16 @@
if
(
!
this
.
comparator
)
{
throw
new
Error
(
'
Cannot sort a set without a comparator
'
);
}
options
||
(
options
=
{});
// Run sort based on type of `comparator`.
if
(
_
.
isString
(
this
.
comparator
)
||
this
.
comparator
.
length
===
1
)
{
this
.
models
=
this
.
sortBy
(
this
.
comparator
,
this
);
}
else
{
this
.
models
.
sort
(
_
.
bind
(
this
.
comparator
,
this
));
}
if
(
!
options
||
!
options
.
silent
)
this
.
trigger
(
'
sort
'
,
this
,
options
);
if
(
!
options
.
silent
)
this
.
trigger
(
'
sort
'
,
this
,
options
);
return
this
;
},
...
...
@@ -779,11 +755,10 @@
// Smartly update a collection with a change set of models, adding,
// removing, and merging as necessary.
update
:
function
(
models
,
options
)
{
options
=
_
.
extend
({
add
:
true
,
merge
:
true
,
remove
:
true
},
options
);
if
(
options
.
parse
)
models
=
this
.
parse
(
models
,
options
);
var
model
,
i
,
l
,
existing
;
var
add
=
[],
remove
=
[],
modelMap
=
{};
var
idAttr
=
this
.
model
.
prototype
.
idAttribute
;
options
=
_
.
extend
({
add
:
true
,
merge
:
true
,
remove
:
true
},
options
);
if
(
options
.
parse
)
models
=
this
.
parse
(
models
);
// Allow a single model (or no argument) to be passed.
if
(
!
_
.
isArray
(
models
))
models
=
models
?
[
models
]
:
[];
...
...
@@ -794,7 +769,7 @@
// Determine which models to add and merge, and which to remove.
for
(
i
=
0
,
l
=
models
.
length
;
i
<
l
;
i
++
)
{
model
=
models
[
i
];
existing
=
this
.
get
(
model
.
id
||
model
.
cid
||
model
[
idAttr
]
);
existing
=
this
.
get
(
model
);
if
(
options
.
remove
&&
existing
)
modelMap
[
existing
.
cid
]
=
true
;
if
((
options
.
add
&&
!
existing
)
||
(
options
.
merge
&&
existing
))
{
add
.
push
(
model
);
...
...
@@ -818,11 +793,11 @@
// any `add` or `remove` events. Fires `reset` when finished.
reset
:
function
(
models
,
options
)
{
options
||
(
options
=
{});
if
(
options
.
parse
)
models
=
this
.
parse
(
models
);
if
(
options
.
parse
)
models
=
this
.
parse
(
models
,
options
);
for
(
var
i
=
0
,
l
=
this
.
models
.
length
;
i
<
l
;
i
++
)
{
this
.
_removeReference
(
this
.
models
[
i
]);
}
options
.
previousModels
=
this
.
models
;
options
.
previousModels
=
this
.
models
.
slice
()
;
this
.
_reset
();
if
(
models
)
this
.
add
(
models
,
_
.
extend
({
silent
:
true
},
options
));
if
(
!
options
.
silent
)
this
.
trigger
(
'
reset
'
,
this
,
options
);
...
...
@@ -830,14 +805,13 @@
},
// Fetch the default set of models for this collection, resetting the
// collection when they arrive. If `
add: true` is passed, appends th
e
//
models to the collection instead of resetting
.
// collection when they arrive. If `
update: true` is passed, the respons
e
//
data will be passed through the `update` method instead of `reset`
.
fetch
:
function
(
options
)
{
options
=
options
?
_
.
clone
(
options
)
:
{};
if
(
options
.
parse
===
void
0
)
options
.
parse
=
true
;
var
collection
=
this
;
var
success
=
options
.
success
;
options
.
success
=
function
(
resp
,
status
,
xhr
)
{
options
.
success
=
function
(
collection
,
resp
,
options
)
{
var
method
=
options
.
update
?
'
update
'
:
'
reset
'
;
collection
[
method
](
resp
,
options
);
if
(
success
)
success
(
collection
,
resp
,
options
);
...
...
@@ -849,11 +823,10 @@
// collection immediately, unless `wait: true` is passed, in which case we
// wait for the server to agree.
create
:
function
(
model
,
options
)
{
var
collection
=
this
;
options
=
options
?
_
.
clone
(
options
)
:
{};
model
=
this
.
_prepareModel
(
model
,
options
)
;
if
(
!
model
)
return
false
;
if
(
!
options
.
wait
)
collection
.
add
(
model
,
options
)
;
if
(
!
(
model
=
this
.
_prepareModel
(
model
,
options
)))
return
false
;
if
(
!
options
.
wait
)
this
.
add
(
model
,
options
)
;
var
collection
=
this
;
var
success
=
options
.
success
;
options
.
success
=
function
(
model
,
resp
,
options
)
{
if
(
options
.
wait
)
collection
.
add
(
model
,
options
);
...
...
@@ -865,7 +838,7 @@
// **parse** converts a response into a list of models to be added to the
// collection. The default implementation is just to pass it through.
parse
:
function
(
resp
)
{
parse
:
function
(
resp
,
options
)
{
return
resp
;
},
...
...
@@ -874,19 +847,11 @@
return
new
this
.
constructor
(
this
.
models
);
},
// Proxy to _'s chain. Can't be proxied the same way the rest of the
// underscore methods are proxied because it relies on the underscore
// constructor.
chain
:
function
()
{
return
_
(
this
.
models
).
chain
();
},
// Reset all internal state. Called when the collection is reset.
_reset
:
function
()
{
this
.
length
=
0
;
this
.
models
=
[]
;
this
.
models
.
length
=
0
;
this
.
_byId
=
{};
this
.
_byCid
=
{};
},
// Prepare a model or hash of attributes to be added to this collection.
...
...
@@ -920,6 +885,14 @@
if
(
model
.
id
!=
null
)
this
.
_byId
[
model
.
id
]
=
model
;
}
this
.
trigger
.
apply
(
this
,
arguments
);
},
sortedIndex
:
function
(
model
,
value
,
context
)
{
value
||
(
value
=
this
.
comparator
);
var
iterator
=
_
.
isFunction
(
value
)
?
value
:
function
(
model
)
{
return
model
.
get
(
value
);
};
return
_
.
sortedIndex
(
this
.
models
,
model
,
iterator
,
context
);
}
});
...
...
@@ -928,9 +901,9 @@
var
methods
=
[
'
forEach
'
,
'
each
'
,
'
map
'
,
'
collect
'
,
'
reduce
'
,
'
foldl
'
,
'
inject
'
,
'
reduceRight
'
,
'
foldr
'
,
'
find
'
,
'
detect
'
,
'
filter
'
,
'
select
'
,
'
reject
'
,
'
every
'
,
'
all
'
,
'
some
'
,
'
any
'
,
'
include
'
,
'
contains
'
,
'
invoke
'
,
'
max
'
,
'
min
'
,
'
sortedIndex
'
,
'
toArray
'
,
'
size
'
,
'
first
'
,
'
head
'
,
'
take
'
,
'
initial
'
,
'
rest
'
,
'
tail
'
,
'
last
'
,
'
without
'
,
'
indexOf
'
,
'
shuffle
'
,
'
lastIndexOf
'
,
'
isEmpty
'
];
'
max
'
,
'
min
'
,
'
toArray
'
,
'
size
'
,
'
first
'
,
'
head
'
,
'
take
'
,
'
initial
'
,
'
rest
'
,
'
tail
'
,
'
drop
'
,
'
last
'
,
'
without
'
,
'
indexOf
'
,
'
shuffle
'
,
'
lastIndexOf
'
,
'
isEmpty
'
,
'
chain
'
];
// Mix in each Underscore method as a proxy to `Collection#models`.
_
.
each
(
methods
,
function
(
method
)
{
...
...
@@ -969,7 +942,7 @@
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var
optionalParam
=
/
\((
.*
?)\)
/g
;
var
namedParam
=
/:
\w
+/g
;
var
namedParam
=
/
(\(\?)?
:
\w
+/g
;
var
splatParam
=
/
\*\w
+/g
;
var
escapeRegExp
=
/
[\-
{}
\[\]
+?.,
\\\^
$|#
\s]
/g
;
...
...
@@ -993,6 +966,7 @@
var
args
=
this
.
_extractParameters
(
route
,
fragment
);
callback
&&
callback
.
apply
(
this
,
args
);
this
.
trigger
.
apply
(
this
,
[
'
route:
'
+
name
].
concat
(
args
));
this
.
trigger
(
'
route
'
,
name
,
args
);
Backbone
.
history
.
trigger
(
'
route
'
,
this
,
name
,
args
);
},
this
));
return
this
;
...
...
@@ -1020,7 +994,9 @@
_routeToRegExp
:
function
(
route
)
{
route
=
route
.
replace
(
escapeRegExp
,
'
\\
$&
'
)
.
replace
(
optionalParam
,
'
(?:$1)?
'
)
.
replace
(
namedParam
,
'
([^
\
/]+)
'
)
.
replace
(
namedParam
,
function
(
match
,
optional
){
return
optional
?
match
:
'
([^
\
/]+)
'
;
})
.
replace
(
splatParam
,
'
(.*?)
'
);
return
new
RegExp
(
'
^
'
+
route
+
'
$
'
);
},
...
...
@@ -1042,7 +1018,7 @@
this
.
handlers
=
[];
_
.
bindAll
(
this
,
'
checkUrl
'
);
//
#1653 -
Ensure that `History` can be used outside of the browser.
// Ensure that `History` can be used outside of the browser.
if
(
typeof
window
!==
'
undefined
'
)
{
this
.
location
=
window
.
location
;
this
.
history
=
window
.
history
;
...
...
@@ -1121,9 +1097,9 @@
// Depending on whether we're using pushState or hashes, and whether
// 'onhashchange' is supported, determine how we check the URL state.
if
(
this
.
_hasPushState
)
{
Backbone
.
$
(
window
).
bind
(
'
popstate
'
,
this
.
checkUrl
);
Backbone
.
$
(
window
).
on
(
'
popstate
'
,
this
.
checkUrl
);
}
else
if
(
this
.
_wantsHashChange
&&
(
'
onhashchange
'
in
window
)
&&
!
oldIE
)
{
Backbone
.
$
(
window
).
bind
(
'
hashchange
'
,
this
.
checkUrl
);
Backbone
.
$
(
window
).
on
(
'
hashchange
'
,
this
.
checkUrl
);
}
else
if
(
this
.
_wantsHashChange
)
{
this
.
_checkUrlInterval
=
setInterval
(
this
.
checkUrl
,
this
.
interval
);
}
...
...
@@ -1155,7 +1131,7 @@
// Disable Backbone.history, perhaps temporarily. Not useful in a real app,
// but possibly useful for unit testing Routers.
stop
:
function
()
{
Backbone
.
$
(
window
).
unbind
(
'
popstate
'
,
this
.
checkUrl
).
unbind
(
'
hashchange
'
,
this
.
checkUrl
);
Backbone
.
$
(
window
).
off
(
'
popstate
'
,
this
.
checkUrl
).
off
(
'
hashchange
'
,
this
.
checkUrl
);
clearInterval
(
this
.
_checkUrlInterval
);
History
.
started
=
false
;
},
...
...
@@ -1238,7 +1214,7 @@
var
href
=
location
.
href
.
replace
(
/
(
javascript:|#
)
.*$/
,
''
);
location
.
replace
(
href
+
'
#
'
+
fragment
);
}
else
{
//
#1649 -
Some browsers require that `hash` contains a leading #.
// Some browsers require that `hash` contains a leading #.
location
.
hash
=
'
#
'
+
fragment
;
}
}
...
...
@@ -1298,18 +1274,6 @@
return
this
;
},
// For small amounts of DOM Elements, where a full-blown template isn't
// needed, use **make** to manufacture elements, one at a time.
//
// var el = this.make('li', {'class': 'row'}, this.model.escape('title'));
//
make
:
function
(
tagName
,
attributes
,
content
)
{
var
el
=
document
.
createElement
(
tagName
);
if
(
attributes
)
Backbone
.
$
(
el
).
attr
(
attributes
);
if
(
content
!=
null
)
Backbone
.
$
(
el
).
html
(
content
);
return
el
;
},
// Change the view's element (`this.el` property), including event
// re-delegation.
setElement
:
function
(
element
,
delegate
)
{
...
...
@@ -1347,9 +1311,9 @@
method
=
_
.
bind
(
method
,
this
);
eventName
+=
'
.delegateEvents
'
+
this
.
cid
;
if
(
selector
===
''
)
{
this
.
$el
.
bind
(
eventName
,
method
);
this
.
$el
.
on
(
eventName
,
method
);
}
else
{
this
.
$el
.
delegate
(
selector
,
eventName
,
method
);
this
.
$el
.
on
(
eventName
,
selector
,
method
);
}
}
},
...
...
@@ -1358,7 +1322,7 @@
// You usually don't need to use this, but may wish to if you have multiple
// Backbone views attached to the same DOM element.
undelegateEvents
:
function
()
{
this
.
$el
.
unbind
(
'
.delegateEvents
'
+
this
.
cid
);
this
.
$el
.
off
(
'
.delegateEvents
'
+
this
.
cid
);
},
// Performs the initial configuration of a View with a set of options.
...
...
@@ -1379,7 +1343,8 @@
var
attrs
=
_
.
extend
({},
_
.
result
(
this
,
'
attributes
'
));
if
(
this
.
id
)
attrs
.
id
=
_
.
result
(
this
,
'
id
'
);
if
(
this
.
className
)
attrs
[
'
class
'
]
=
_
.
result
(
this
,
'
className
'
);
this
.
setElement
(
this
.
make
(
_
.
result
(
this
,
'
tagName
'
),
attrs
),
false
);
var
$el
=
Backbone
.
$
(
'
<
'
+
_
.
result
(
this
,
'
tagName
'
)
+
'
>
'
).
attr
(
attrs
);
this
.
setElement
(
$el
,
false
);
}
else
{
this
.
setElement
(
_
.
result
(
this
,
'
el
'
),
false
);
}
...
...
@@ -1461,19 +1426,19 @@
}
var
success
=
options
.
success
;
options
.
success
=
function
(
resp
,
status
,
xhr
)
{
if
(
success
)
success
(
resp
,
status
,
xhr
);
options
.
success
=
function
(
resp
)
{
if
(
success
)
success
(
model
,
resp
,
options
);
model
.
trigger
(
'
sync
'
,
model
,
resp
,
options
);
};
var
error
=
options
.
error
;
options
.
error
=
function
(
xhr
,
status
,
thrown
)
{
options
.
error
=
function
(
xhr
)
{
if
(
error
)
error
(
model
,
xhr
,
options
);
model
.
trigger
(
'
error
'
,
model
,
xhr
,
options
);
};
// Make the request, allowing the user to override any Ajax options.
var
xhr
=
Backbone
.
ajax
(
_
.
extend
(
params
,
options
));
var
xhr
=
options
.
xhr
=
Backbone
.
ajax
(
_
.
extend
(
params
,
options
));
model
.
trigger
(
'
request
'
,
model
,
xhr
,
options
);
return
xhr
;
};
...
...
@@ -1499,7 +1464,7 @@
if
(
protoProps
&&
_
.
has
(
protoProps
,
'
constructor
'
))
{
child
=
protoProps
.
constructor
;
}
else
{
child
=
function
(){
parent
.
apply
(
this
,
arguments
);
};
child
=
function
(){
return
parent
.
apply
(
this
,
arguments
);
};
}
// Add static properties to the constructor function, if supplied.
...
...
dependency-examples/backbone_require/components/jquery/jquery.js
0 → 100644
View file @
d501a8f5
This source diff could not be displayed because it is too large. You can
view the blob
instead.
dependency-examples/backbone_require/
js/lib/require
/text.js
→
dependency-examples/backbone_require/
components/requirejs-text
/text.js
View file @
d501a8f5
/**
* @license RequireJS text 2.0.
3
Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* @license RequireJS text 2.0.
5
Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/requirejs/text for details
*/
...
...
@@ -23,7 +23,7 @@ define(['module'], function (module) {
masterConfig
=
(
module
.
config
&&
module
.
config
())
||
{};
text
=
{
version
:
'
2.0.
3
'
,
version
:
'
2.0.
5
'
,
strip
:
function
(
content
)
{
//Strips <?xml ...?> declarations so that external SVG and XML
...
...
@@ -83,16 +83,30 @@ define(['module'], function (module) {
* where strip is a boolean.
*/
parseName
:
function
(
name
)
{
var
strip
=
false
,
index
=
name
.
indexOf
(
"
.
"
),
modName
=
name
.
substring
(
0
,
index
),
var
modName
,
ext
,
temp
,
strip
=
false
,
index
=
name
.
indexOf
(
"
.
"
),
isRelative
=
name
.
indexOf
(
'
./
'
)
===
0
||
name
.
indexOf
(
'
../
'
)
===
0
;
if
(
index
!==
-
1
&&
(
!
isRelative
||
index
>
1
))
{
modName
=
name
.
substring
(
0
,
index
);
ext
=
name
.
substring
(
index
+
1
,
name
.
length
);
}
else
{
modName
=
name
;
}
index
=
ext
.
indexOf
(
"
!
"
);
temp
=
ext
||
modName
;
index
=
temp
.
indexOf
(
"
!
"
);
if
(
index
!==
-
1
)
{
//Pull off the strip arg.
strip
=
ext
.
substring
(
index
+
1
,
ext
.
length
);
strip
=
strip
===
"
strip
"
;
ext
=
ext
.
substring
(
0
,
index
);
strip
=
temp
.
substring
(
index
+
1
)
===
"
strip
"
;
temp
=
temp
.
substring
(
0
,
index
);
if
(
ext
)
{
ext
=
temp
;
}
else
{
modName
=
temp
;
}
}
return
{
...
...
@@ -156,7 +170,8 @@ define(['module'], function (module) {
masterConfig
.
isBuild
=
config
.
isBuild
;
var
parsed
=
text
.
parseName
(
name
),
nonStripName
=
parsed
.
moduleName
+
'
.
'
+
parsed
.
ext
,
nonStripName
=
parsed
.
moduleName
+
(
parsed
.
ext
?
'
.
'
+
parsed
.
ext
:
''
),
url
=
req
.
toUrl
(
nonStripName
),
useXhr
=
(
masterConfig
.
useXhr
)
||
text
.
useXhr
;
...
...
@@ -194,11 +209,11 @@ define(['module'], function (module) {
writeFile
:
function
(
pluginName
,
moduleName
,
req
,
write
,
config
)
{
var
parsed
=
text
.
parseName
(
moduleName
),
nonStripName
=
parsed
.
moduleName
+
'
.
'
+
parsed
.
ext
,
extPart
=
parsed
.
ext
?
'
.
'
+
parsed
.
ext
:
''
,
nonStripName
=
parsed
.
moduleName
+
extPart
,
//Use a '.js' file name so that it indicates it is a
//script that can be loaded across domains.
fileName
=
req
.
toUrl
(
parsed
.
moduleName
+
'
.
'
+
parsed
.
ext
)
+
'
.js
'
;
fileName
=
req
.
toUrl
(
parsed
.
moduleName
+
extPart
)
+
'
.js
'
;
//Leverage own load() method to load plugin value, but only
//write out values that do not have the strip argument,
...
...
@@ -236,10 +251,19 @@ define(['module'], function (module) {
};
}
else
if
(
masterConfig
.
env
===
'
xhr
'
||
(
!
masterConfig
.
env
&&
text
.
createXhr
()))
{
text
.
get
=
function
(
url
,
callback
,
errback
)
{
var
xhr
=
text
.
createXhr
();
text
.
get
=
function
(
url
,
callback
,
errback
,
headers
)
{
var
xhr
=
text
.
createXhr
()
,
header
;
xhr
.
open
(
'
GET
'
,
url
,
true
);
//Allow plugins direct access to xhr headers
if
(
headers
)
{
for
(
header
in
headers
)
{
if
(
headers
.
hasOwnProperty
(
header
))
{
xhr
.
setRequestHeader
(
header
.
toLowerCase
(),
headers
[
header
]);
}
}
}
//Allow overrides specified in config
if
(
masterConfig
.
onXhr
)
{
masterConfig
.
onXhr
(
xhr
,
url
);
...
...
dependency-examples/backbone_require/
js/lib/require
/require.js
→
dependency-examples/backbone_require/
components/requirejs
/require.js
View file @
d501a8f5
/** vim: et:ts=4:sw=4:sts=4
* @license RequireJS 2.1.
2
Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* @license RequireJS 2.1.
5
Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
//Not using strict: uneven strict support in browsers, #392, and causes
//problems with requirejs.exec()/transpiler plugins that may not be strict.
/*jslint regexp: true, nomen: true, sloppy: true */
/*global window, navigator, document, importScripts,
jQuery,
setTimeout, opera */
/*global window, navigator, document, importScripts, setTimeout, opera */
var
requirejs
,
require
,
define
;
(
function
(
global
)
{
var
req
,
s
,
head
,
baseElement
,
dataMain
,
src
,
interactiveScript
,
currentlyAddingScript
,
mainScript
,
subPath
,
version
=
'
2.1.
2
'
,
version
=
'
2.1.
5
'
,
commentRegExp
=
/
(\/\*([\s\S]
*
?)\*\/
|
([^
:
]
|^
)\/\/(
.*
)
$
)
/mg
,
cjsRequireRegExp
=
/
[^
.
]\s
*require
\s
*
\(\s
*
[
"'
]([^
'"
\s]
+
)[
"'
]\s
*
\)
/g
,
jsSuffixRegExp
=
/
\.
js$/
,
...
...
@@ -21,7 +21,6 @@ var requirejs, require, define;
ostring
=
op
.
toString
,
hasOwn
=
op
.
hasOwnProperty
,
ap
=
Array
.
prototype
,
aps
=
ap
.
slice
,
apsp
=
ap
.
splice
,
isBrowser
=
!!
(
typeof
window
!==
'
undefined
'
&&
navigator
&&
document
),
isWebWorker
=
!
isBrowser
&&
typeof
importScripts
!==
'
undefined
'
,
...
...
@@ -192,15 +191,21 @@ var requirejs, require, define;
var
inCheckLoaded
,
Module
,
context
,
handlers
,
checkLoadedTimeoutId
,
config
=
{
//Defaults. Do not set a default for map
//config to speed up normalize(), which
//will run faster if there is no default.
waitSeconds
:
7
,
baseUrl
:
'
./
'
,
paths
:
{},
pkgs
:
{},
shim
:
{},
map
:
{},
config
:
{}
},
registry
=
{},
//registry of just enabled modules, to speed
//cycle breaking code when lots of modules
//are registered, but not activated.
enabledRegistry
=
{},
undefEvents
=
{},
defQueue
=
[],
defined
=
{},
...
...
@@ -296,7 +301,7 @@ var requirejs, require, define;
}
//Apply map config if available.
if
(
applyMap
&&
(
baseParts
||
starMap
)
&&
map
)
{
if
(
applyMap
&&
map
&&
(
baseParts
||
starMap
)
)
{
nameParts
=
name
.
split
(
'
/
'
);
for
(
i
=
nameParts
.
length
;
i
>
0
;
i
-=
1
)
{
...
...
@@ -577,6 +582,7 @@ var requirejs, require, define;
function
cleanRegistry
(
id
)
{
//Clean up machinery used for waiting modules.
delete
registry
[
id
];
delete
enabledRegistry
[
id
];
}
function
breakCycle
(
mod
,
traced
,
processed
)
{
...
...
@@ -625,7 +631,7 @@ var requirejs, require, define;
inCheckLoaded
=
true
;
//Figure out the state of all the modules.
eachProp
(
r
egistry
,
function
(
mod
)
{
eachProp
(
enabledR
egistry
,
function
(
mod
)
{
map
=
mod
.
map
;
modId
=
map
.
id
;
...
...
@@ -806,7 +812,7 @@ var requirejs, require, define;
},
/**
* Checks i
s
the module is ready to define itself, and if so,
* Checks i
f
the module is ready to define itself, and if so,
* define it.
*/
check
:
function
()
{
...
...
@@ -884,7 +890,7 @@ var requirejs, require, define;
}
//Clean up
delete
registry
[
id
]
;
cleanRegistry
(
id
)
;
this
.
defined
=
true
;
}
...
...
@@ -918,8 +924,7 @@ var requirejs, require, define;
name
=
this
.
map
.
name
,
parentName
=
this
.
map
.
parentMap
?
this
.
map
.
parentMap
.
name
:
null
,
localRequire
=
context
.
makeRequire
(
map
.
parentMap
,
{
enableBuildCallback
:
true
,
skipMap
:
true
enableBuildCallback
:
true
});
//If current map is not normalized, wait for that
...
...
@@ -1017,8 +1022,11 @@ var requirejs, require, define;
try
{
req
.
exec
(
text
);
}
catch
(
e
)
{
throw
new
Error
(
'
fromText eval for
'
+
moduleName
+
'
failed:
'
+
e
);
return
onError
(
makeError
(
'
fromtexteval
'
,
'
fromText eval for
'
+
id
+
'
failed:
'
+
e
,
e
,
[
id
]));
}
if
(
hasInteractive
)
{
...
...
@@ -1048,6 +1056,7 @@ var requirejs, require, define;
},
enable
:
function
()
{
enabledRegistry
[
this
.
map
.
id
]
=
this
;
this
.
enabled
=
true
;
//Set flag mentioning that the module is enabling,
...
...
@@ -1207,6 +1216,7 @@ var requirejs, require, define;
Module
:
Module
,
makeModuleMap
:
makeModuleMap
,
nextTick
:
req
.
nextTick
,
onError
:
onError
,
/**
* Set a configuration for the context.
...
...
@@ -1233,6 +1243,9 @@ var requirejs, require, define;
eachProp
(
cfg
,
function
(
value
,
prop
)
{
if
(
objs
[
prop
])
{
if
(
prop
===
'
map
'
)
{
if
(
!
config
.
map
)
{
config
.
map
=
{};
}
mixin
(
config
[
prop
],
value
,
true
,
true
);
}
else
{
mixin
(
config
[
prop
],
value
,
true
);
...
...
@@ -1344,7 +1357,7 @@ var requirejs, require, define;
//Synchronous access to one module. If require.get is
//available (as in the Node adapter), prefer that.
if
(
req
.
get
)
{
return
req
.
get
(
context
,
deps
,
relMap
);
return
req
.
get
(
context
,
deps
,
relMap
,
localRequire
);
}
//Normalize module name, if it contains . or ..
...
...
@@ -1395,16 +1408,20 @@ var requirejs, require, define;
* plain URLs like nameToUrl.
*/
toUrl
:
function
(
moduleNamePlusExt
)
{
var
index
=
moduleNamePlusExt
.
lastIndexOf
(
'
.
'
),
ext
=
null
;
if
(
index
!==
-
1
)
{
var
ext
,
index
=
moduleNamePlusExt
.
lastIndexOf
(
'
.
'
),
segment
=
moduleNamePlusExt
.
split
(
'
/
'
)[
0
],
isRelative
=
segment
===
'
.
'
||
segment
===
'
..
'
;
//Have a file extension alias, and it is not the
//dots from a relative path.
if
(
index
!==
-
1
&&
(
!
isRelative
||
index
>
1
))
{
ext
=
moduleNamePlusExt
.
substring
(
index
,
moduleNamePlusExt
.
length
);
moduleNamePlusExt
=
moduleNamePlusExt
.
substring
(
0
,
index
);
}
return
context
.
nameToUrl
(
normalize
(
moduleNamePlusExt
,
relMap
&&
relMap
.
id
,
true
),
ext
);
relMap
&&
relMap
.
id
,
true
),
ext
,
true
);
},
defined
:
function
(
id
)
{
...
...
@@ -1449,10 +1466,11 @@ var requirejs, require, define;
/**
* Called to enable a module if it is still in the registry
* awaiting enablement. parent module is passed in for context,
* used by the optimizer.
* awaiting enablement. A second arg, parent, the parent module,
* is passed in for context, when this method is overriden by
* the optimizer. Not shown here to keep code compact.
*/
enable
:
function
(
depMap
,
parent
)
{
enable
:
function
(
depMap
)
{
var
mod
=
getOwn
(
registry
,
depMap
.
id
);
if
(
mod
)
{
getModule
(
depMap
).
enable
();
...
...
@@ -1522,7 +1540,7 @@ var requirejs, require, define;
* it is assumed to have already been normalized. This is an
* internal API, not a public one. Use toUrl for the public API.
*/
nameToUrl
:
function
(
moduleName
,
ext
)
{
nameToUrl
:
function
(
moduleName
,
ext
,
skipExt
)
{
var
paths
,
pkgs
,
pkg
,
pkgPath
,
syms
,
i
,
parentModule
,
url
,
parentPath
;
...
...
@@ -1571,7 +1589,7 @@ var requirejs, require, define;
//Join the path parts together, then figure out if baseUrl is needed.
url
=
syms
.
join
(
'
/
'
);
url
+=
(
ext
||
(
/
\?
/
.
test
(
url
)
?
''
:
'
.js
'
));
url
+=
(
ext
||
(
/
\?
/
.
test
(
url
)
||
skipExt
?
''
:
'
.js
'
));
url
=
(
url
.
charAt
(
0
)
===
'
/
'
||
url
.
match
(
/^
[\w\+\.\-]
+:/
)
?
''
:
config
.
baseUrl
)
+
url
;
}
...
...
@@ -1810,7 +1828,7 @@ var requirejs, require, define;
node
.
attachEvent
(
'
onreadystatechange
'
,
context
.
onScriptLoad
);
//It would be great to add an error handler here to catch
//404s in IE9+. However, onreadystatechange will fire before
//the error handler, so that does not help. If addEvenListener
//the error handler, so that does not help. If addEven
t
Listener
//is used, then IE will fire error before load, but we cannot
//use that pathway given the connect.microsoft.com issue
//mentioned above about not doing the 'script execute,
...
...
@@ -1839,6 +1857,7 @@ var requirejs, require, define;
return
node
;
}
else
if
(
isWebWorker
)
{
try
{
//In a web worker, use importScripts. This is not a very
//efficient use of importScripts, importScripts will block until
//its script is downloaded and evaluated. However, if web workers
...
...
@@ -1849,6 +1868,13 @@ var requirejs, require, define;
//Account for anonymous modules
context
.
completeLoad
(
moduleName
);
}
catch
(
e
)
{
context
.
onError
(
makeError
(
'
importscripts
'
,
'
importScripts failed for
'
+
moduleName
+
'
at
'
+
url
,
e
,
[
moduleName
]));
}
}
};
...
...
dependency-examples/backbone_require/components/todomvc-common/base.js
0 → 100644
View file @
d501a8f5
(
function
()
{
'
use strict
'
;
if
(
location
.
hostname
===
'
todomvc.com
'
)
{
var
_gaq
=
[[
'
_setAccount
'
,
'
UA-31081062-1
'
],[
'
_trackPageview
'
]];(
function
(
d
,
t
){
var
g
=
d
.
createElement
(
t
),
s
=
d
.
getElementsByTagName
(
t
)[
0
];
g
.
src
=
'
//www.google-analytics.com/ga.js
'
;
s
.
parentNode
.
insertBefore
(
g
,
s
)}(
document
,
'
script
'
));
}
})();
dependency-examples/backbone_require/components/underscore/underscore.js
0 → 100644
View file @
d501a8f5
// Underscore.js 1.4.4
// http://underscorejs.org
// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
// Underscore may be freely distributed under the MIT license.
(
function
()
{
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `global` on the server.
var
root
=
this
;
// Save the previous value of the `_` variable.
var
previousUnderscore
=
root
.
_
;
// Establish the object that gets returned to break out of a loop iteration.
var
breaker
=
{};
// Save bytes in the minified (but not gzipped) version:
var
ArrayProto
=
Array
.
prototype
,
ObjProto
=
Object
.
prototype
,
FuncProto
=
Function
.
prototype
;
// Create quick reference variables for speed access to core prototypes.
var
push
=
ArrayProto
.
push
,
slice
=
ArrayProto
.
slice
,
concat
=
ArrayProto
.
concat
,
toString
=
ObjProto
.
toString
,
hasOwnProperty
=
ObjProto
.
hasOwnProperty
;
// All **ECMAScript 5** native function implementations that we hope to use
// are declared here.
var
nativeForEach
=
ArrayProto
.
forEach
,
nativeMap
=
ArrayProto
.
map
,
nativeReduce
=
ArrayProto
.
reduce
,
nativeReduceRight
=
ArrayProto
.
reduceRight
,
nativeFilter
=
ArrayProto
.
filter
,
nativeEvery
=
ArrayProto
.
every
,
nativeSome
=
ArrayProto
.
some
,
nativeIndexOf
=
ArrayProto
.
indexOf
,
nativeLastIndexOf
=
ArrayProto
.
lastIndexOf
,
nativeIsArray
=
Array
.
isArray
,
nativeKeys
=
Object
.
keys
,
nativeBind
=
FuncProto
.
bind
;
// Create a safe reference to the Underscore object for use below.
var
_
=
function
(
obj
)
{
if
(
obj
instanceof
_
)
return
obj
;
if
(
!
(
this
instanceof
_
))
return
new
_
(
obj
);
this
.
_wrapped
=
obj
;
};
// Export the Underscore object for **Node.js**, with
// backwards-compatibility for the old `require()` API. If we're in
// the browser, add `_` as a global object via a string identifier,
// for Closure Compiler "advanced" mode.
if
(
typeof
exports
!==
'
undefined
'
)
{
if
(
typeof
module
!==
'
undefined
'
&&
module
.
exports
)
{
exports
=
module
.
exports
=
_
;
}
exports
.
_
=
_
;
}
else
{
root
.
_
=
_
;
}
// Current version.
_
.
VERSION
=
'
1.4.4
'
;
// Collection Functions
// --------------------
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var
each
=
_
.
each
=
_
.
forEach
=
function
(
obj
,
iterator
,
context
)
{
if
(
obj
==
null
)
return
;
if
(
nativeForEach
&&
obj
.
forEach
===
nativeForEach
)
{
obj
.
forEach
(
iterator
,
context
);
}
else
if
(
obj
.
length
===
+
obj
.
length
)
{
for
(
var
i
=
0
,
l
=
obj
.
length
;
i
<
l
;
i
++
)
{
if
(
iterator
.
call
(
context
,
obj
[
i
],
i
,
obj
)
===
breaker
)
return
;
}
}
else
{
for
(
var
key
in
obj
)
{
if
(
_
.
has
(
obj
,
key
))
{
if
(
iterator
.
call
(
context
,
obj
[
key
],
key
,
obj
)
===
breaker
)
return
;
}
}
}
};
// Return the results of applying the iterator to each element.
// Delegates to **ECMAScript 5**'s native `map` if available.
_
.
map
=
_
.
collect
=
function
(
obj
,
iterator
,
context
)
{
var
results
=
[];
if
(
obj
==
null
)
return
results
;
if
(
nativeMap
&&
obj
.
map
===
nativeMap
)
return
obj
.
map
(
iterator
,
context
);
each
(
obj
,
function
(
value
,
index
,
list
)
{
results
[
results
.
length
]
=
iterator
.
call
(
context
,
value
,
index
,
list
);
});
return
results
;
};
var
reduceError
=
'
Reduce of empty array with no initial value
'
;
// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
_
.
reduce
=
_
.
foldl
=
_
.
inject
=
function
(
obj
,
iterator
,
memo
,
context
)
{
var
initial
=
arguments
.
length
>
2
;
if
(
obj
==
null
)
obj
=
[];
if
(
nativeReduce
&&
obj
.
reduce
===
nativeReduce
)
{
if
(
context
)
iterator
=
_
.
bind
(
iterator
,
context
);
return
initial
?
obj
.
reduce
(
iterator
,
memo
)
:
obj
.
reduce
(
iterator
);
}
each
(
obj
,
function
(
value
,
index
,
list
)
{
if
(
!
initial
)
{
memo
=
value
;
initial
=
true
;
}
else
{
memo
=
iterator
.
call
(
context
,
memo
,
value
,
index
,
list
);
}
});
if
(
!
initial
)
throw
new
TypeError
(
reduceError
);
return
memo
;
};
// The right-associative version of reduce, also known as `foldr`.
// Delegates to **ECMAScript 5**'s native `reduceRight` if available.
_
.
reduceRight
=
_
.
foldr
=
function
(
obj
,
iterator
,
memo
,
context
)
{
var
initial
=
arguments
.
length
>
2
;
if
(
obj
==
null
)
obj
=
[];
if
(
nativeReduceRight
&&
obj
.
reduceRight
===
nativeReduceRight
)
{
if
(
context
)
iterator
=
_
.
bind
(
iterator
,
context
);
return
initial
?
obj
.
reduceRight
(
iterator
,
memo
)
:
obj
.
reduceRight
(
iterator
);
}
var
length
=
obj
.
length
;
if
(
length
!==
+
length
)
{
var
keys
=
_
.
keys
(
obj
);
length
=
keys
.
length
;
}
each
(
obj
,
function
(
value
,
index
,
list
)
{
index
=
keys
?
keys
[
--
length
]
:
--
length
;
if
(
!
initial
)
{
memo
=
obj
[
index
];
initial
=
true
;
}
else
{
memo
=
iterator
.
call
(
context
,
memo
,
obj
[
index
],
index
,
list
);
}
});
if
(
!
initial
)
throw
new
TypeError
(
reduceError
);
return
memo
;
};
// Return the first value which passes a truth test. Aliased as `detect`.
_
.
find
=
_
.
detect
=
function
(
obj
,
iterator
,
context
)
{
var
result
;
any
(
obj
,
function
(
value
,
index
,
list
)
{
if
(
iterator
.
call
(
context
,
value
,
index
,
list
))
{
result
=
value
;
return
true
;
}
});
return
result
;
};
// Return all the elements that pass a truth test.
// Delegates to **ECMAScript 5**'s native `filter` if available.
// Aliased as `select`.
_
.
filter
=
_
.
select
=
function
(
obj
,
iterator
,
context
)
{
var
results
=
[];
if
(
obj
==
null
)
return
results
;
if
(
nativeFilter
&&
obj
.
filter
===
nativeFilter
)
return
obj
.
filter
(
iterator
,
context
);
each
(
obj
,
function
(
value
,
index
,
list
)
{
if
(
iterator
.
call
(
context
,
value
,
index
,
list
))
results
[
results
.
length
]
=
value
;
});
return
results
;
};
// Return all the elements for which a truth test fails.
_
.
reject
=
function
(
obj
,
iterator
,
context
)
{
return
_
.
filter
(
obj
,
function
(
value
,
index
,
list
)
{
return
!
iterator
.
call
(
context
,
value
,
index
,
list
);
},
context
);
};
// Determine whether all of the elements match a truth test.
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
_
.
every
=
_
.
all
=
function
(
obj
,
iterator
,
context
)
{
iterator
||
(
iterator
=
_
.
identity
);
var
result
=
true
;
if
(
obj
==
null
)
return
result
;
if
(
nativeEvery
&&
obj
.
every
===
nativeEvery
)
return
obj
.
every
(
iterator
,
context
);
each
(
obj
,
function
(
value
,
index
,
list
)
{
if
(
!
(
result
=
result
&&
iterator
.
call
(
context
,
value
,
index
,
list
)))
return
breaker
;
});
return
!!
result
;
};
// Determine if at least one element in the object matches a truth test.
// Delegates to **ECMAScript 5**'s native `some` if available.
// Aliased as `any`.
var
any
=
_
.
some
=
_
.
any
=
function
(
obj
,
iterator
,
context
)
{
iterator
||
(
iterator
=
_
.
identity
);
var
result
=
false
;
if
(
obj
==
null
)
return
result
;
if
(
nativeSome
&&
obj
.
some
===
nativeSome
)
return
obj
.
some
(
iterator
,
context
);
each
(
obj
,
function
(
value
,
index
,
list
)
{
if
(
result
||
(
result
=
iterator
.
call
(
context
,
value
,
index
,
list
)))
return
breaker
;
});
return
!!
result
;
};
// Determine if the array or object contains a given value (using `===`).
// Aliased as `include`.
_
.
contains
=
_
.
include
=
function
(
obj
,
target
)
{
if
(
obj
==
null
)
return
false
;
if
(
nativeIndexOf
&&
obj
.
indexOf
===
nativeIndexOf
)
return
obj
.
indexOf
(
target
)
!=
-
1
;
return
any
(
obj
,
function
(
value
)
{
return
value
===
target
;
});
};
// Invoke a method (with arguments) on every item in a collection.
_
.
invoke
=
function
(
obj
,
method
)
{
var
args
=
slice
.
call
(
arguments
,
2
);
var
isFunc
=
_
.
isFunction
(
method
);
return
_
.
map
(
obj
,
function
(
value
)
{
return
(
isFunc
?
method
:
value
[
method
]).
apply
(
value
,
args
);
});
};
// Convenience version of a common use case of `map`: fetching a property.
_
.
pluck
=
function
(
obj
,
key
)
{
return
_
.
map
(
obj
,
function
(
value
){
return
value
[
key
];
});
};
// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_
.
where
=
function
(
obj
,
attrs
,
first
)
{
if
(
_
.
isEmpty
(
attrs
))
return
first
?
null
:
[];
return
_
[
first
?
'
find
'
:
'
filter
'
](
obj
,
function
(
value
)
{
for
(
var
key
in
attrs
)
{
if
(
attrs
[
key
]
!==
value
[
key
])
return
false
;
}
return
true
;
});
};
// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
_
.
findWhere
=
function
(
obj
,
attrs
)
{
return
_
.
where
(
obj
,
attrs
,
true
);
};
// Return the maximum element or (element-based computation).
// Can't optimize arrays of integers longer than 65,535 elements.
// See: https://bugs.webkit.org/show_bug.cgi?id=80797
_
.
max
=
function
(
obj
,
iterator
,
context
)
{
if
(
!
iterator
&&
_
.
isArray
(
obj
)
&&
obj
[
0
]
===
+
obj
[
0
]
&&
obj
.
length
<
65535
)
{
return
Math
.
max
.
apply
(
Math
,
obj
);
}
if
(
!
iterator
&&
_
.
isEmpty
(
obj
))
return
-
Infinity
;
var
result
=
{
computed
:
-
Infinity
,
value
:
-
Infinity
};
each
(
obj
,
function
(
value
,
index
,
list
)
{
var
computed
=
iterator
?
iterator
.
call
(
context
,
value
,
index
,
list
)
:
value
;
computed
>=
result
.
computed
&&
(
result
=
{
value
:
value
,
computed
:
computed
});
});
return
result
.
value
;
};
// Return the minimum element (or element-based computation).
_
.
min
=
function
(
obj
,
iterator
,
context
)
{
if
(
!
iterator
&&
_
.
isArray
(
obj
)
&&
obj
[
0
]
===
+
obj
[
0
]
&&
obj
.
length
<
65535
)
{
return
Math
.
min
.
apply
(
Math
,
obj
);
}
if
(
!
iterator
&&
_
.
isEmpty
(
obj
))
return
Infinity
;
var
result
=
{
computed
:
Infinity
,
value
:
Infinity
};
each
(
obj
,
function
(
value
,
index
,
list
)
{
var
computed
=
iterator
?
iterator
.
call
(
context
,
value
,
index
,
list
)
:
value
;
computed
<
result
.
computed
&&
(
result
=
{
value
:
value
,
computed
:
computed
});
});
return
result
.
value
;
};
// Shuffle an array.
_
.
shuffle
=
function
(
obj
)
{
var
rand
;
var
index
=
0
;
var
shuffled
=
[];
each
(
obj
,
function
(
value
)
{
rand
=
_
.
random
(
index
++
);
shuffled
[
index
-
1
]
=
shuffled
[
rand
];
shuffled
[
rand
]
=
value
;
});
return
shuffled
;
};
// An internal function to generate lookup iterators.
var
lookupIterator
=
function
(
value
)
{
return
_
.
isFunction
(
value
)
?
value
:
function
(
obj
){
return
obj
[
value
];
};
};
// Sort the object's values by a criterion produced by an iterator.
_
.
sortBy
=
function
(
obj
,
value
,
context
)
{
var
iterator
=
lookupIterator
(
value
);
return
_
.
pluck
(
_
.
map
(
obj
,
function
(
value
,
index
,
list
)
{
return
{
value
:
value
,
index
:
index
,
criteria
:
iterator
.
call
(
context
,
value
,
index
,
list
)
};
}).
sort
(
function
(
left
,
right
)
{
var
a
=
left
.
criteria
;
var
b
=
right
.
criteria
;
if
(
a
!==
b
)
{
if
(
a
>
b
||
a
===
void
0
)
return
1
;
if
(
a
<
b
||
b
===
void
0
)
return
-
1
;
}
return
left
.
index
<
right
.
index
?
-
1
:
1
;
}),
'
value
'
);
};
// An internal function used for aggregate "group by" operations.
var
group
=
function
(
obj
,
value
,
context
,
behavior
)
{
var
result
=
{};
var
iterator
=
lookupIterator
(
value
||
_
.
identity
);
each
(
obj
,
function
(
value
,
index
)
{
var
key
=
iterator
.
call
(
context
,
value
,
index
,
obj
);
behavior
(
result
,
key
,
value
);
});
return
result
;
};
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_
.
groupBy
=
function
(
obj
,
value
,
context
)
{
return
group
(
obj
,
value
,
context
,
function
(
result
,
key
,
value
)
{
(
_
.
has
(
result
,
key
)
?
result
[
key
]
:
(
result
[
key
]
=
[])).
push
(
value
);
});
};
// Counts instances of an object that group by a certain criterion. Pass
// either a string attribute to count by, or a function that returns the
// criterion.
_
.
countBy
=
function
(
obj
,
value
,
context
)
{
return
group
(
obj
,
value
,
context
,
function
(
result
,
key
)
{
if
(
!
_
.
has
(
result
,
key
))
result
[
key
]
=
0
;
result
[
key
]
++
;
});
};
// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
_
.
sortedIndex
=
function
(
array
,
obj
,
iterator
,
context
)
{
iterator
=
iterator
==
null
?
_
.
identity
:
lookupIterator
(
iterator
);
var
value
=
iterator
.
call
(
context
,
obj
);
var
low
=
0
,
high
=
array
.
length
;
while
(
low
<
high
)
{
var
mid
=
(
low
+
high
)
>>>
1
;
iterator
.
call
(
context
,
array
[
mid
])
<
value
?
low
=
mid
+
1
:
high
=
mid
;
}
return
low
;
};
// Safely convert anything iterable into a real, live array.
_
.
toArray
=
function
(
obj
)
{
if
(
!
obj
)
return
[];
if
(
_
.
isArray
(
obj
))
return
slice
.
call
(
obj
);
if
(
obj
.
length
===
+
obj
.
length
)
return
_
.
map
(
obj
,
_
.
identity
);
return
_
.
values
(
obj
);
};
// Return the number of elements in an object.
_
.
size
=
function
(
obj
)
{
if
(
obj
==
null
)
return
0
;
return
(
obj
.
length
===
+
obj
.
length
)
?
obj
.
length
:
_
.
keys
(
obj
).
length
;
};
// Array Functions
// ---------------
// Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head` and `take`. The **guard** check
// allows it to work with `_.map`.
_
.
first
=
_
.
head
=
_
.
take
=
function
(
array
,
n
,
guard
)
{
if
(
array
==
null
)
return
void
0
;
return
(
n
!=
null
)
&&
!
guard
?
slice
.
call
(
array
,
0
,
n
)
:
array
[
0
];
};
// Returns everything but the last entry of the array. Especially useful on
// the arguments object. Passing **n** will return all the values in
// the array, excluding the last N. The **guard** check allows it to work with
// `_.map`.
_
.
initial
=
function
(
array
,
n
,
guard
)
{
return
slice
.
call
(
array
,
0
,
array
.
length
-
((
n
==
null
)
||
guard
?
1
:
n
));
};
// Get the last element of an array. Passing **n** will return the last N
// values in the array. The **guard** check allows it to work with `_.map`.
_
.
last
=
function
(
array
,
n
,
guard
)
{
if
(
array
==
null
)
return
void
0
;
if
((
n
!=
null
)
&&
!
guard
)
{
return
slice
.
call
(
array
,
Math
.
max
(
array
.
length
-
n
,
0
));
}
else
{
return
array
[
array
.
length
-
1
];
}
};
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
// Especially useful on the arguments object. Passing an **n** will return
// the rest N values in the array. The **guard**
// check allows it to work with `_.map`.
_
.
rest
=
_
.
tail
=
_
.
drop
=
function
(
array
,
n
,
guard
)
{
return
slice
.
call
(
array
,
(
n
==
null
)
||
guard
?
1
:
n
);
};
// Trim out all falsy values from an array.
_
.
compact
=
function
(
array
)
{
return
_
.
filter
(
array
,
_
.
identity
);
};
// Internal implementation of a recursive `flatten` function.
var
flatten
=
function
(
input
,
shallow
,
output
)
{
each
(
input
,
function
(
value
)
{
if
(
_
.
isArray
(
value
))
{
shallow
?
push
.
apply
(
output
,
value
)
:
flatten
(
value
,
shallow
,
output
);
}
else
{
output
.
push
(
value
);
}
});
return
output
;
};
// Return a completely flattened version of an array.
_
.
flatten
=
function
(
array
,
shallow
)
{
return
flatten
(
array
,
shallow
,
[]);
};
// Return a version of the array that does not contain the specified value(s).
_
.
without
=
function
(
array
)
{
return
_
.
difference
(
array
,
slice
.
call
(
arguments
,
1
));
};
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_
.
uniq
=
_
.
unique
=
function
(
array
,
isSorted
,
iterator
,
context
)
{
if
(
_
.
isFunction
(
isSorted
))
{
context
=
iterator
;
iterator
=
isSorted
;
isSorted
=
false
;
}
var
initial
=
iterator
?
_
.
map
(
array
,
iterator
,
context
)
:
array
;
var
results
=
[];
var
seen
=
[];
each
(
initial
,
function
(
value
,
index
)
{
if
(
isSorted
?
(
!
index
||
seen
[
seen
.
length
-
1
]
!==
value
)
:
!
_
.
contains
(
seen
,
value
))
{
seen
.
push
(
value
);
results
.
push
(
array
[
index
]);
}
});
return
results
;
};
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_
.
union
=
function
()
{
return
_
.
uniq
(
concat
.
apply
(
ArrayProto
,
arguments
));
};
// Produce an array that contains every item shared between all the
// passed-in arrays.
_
.
intersection
=
function
(
array
)
{
var
rest
=
slice
.
call
(
arguments
,
1
);
return
_
.
filter
(
_
.
uniq
(
array
),
function
(
item
)
{
return
_
.
every
(
rest
,
function
(
other
)
{
return
_
.
indexOf
(
other
,
item
)
>=
0
;
});
});
};
// Take the difference between one array and a number of other arrays.
// Only the elements present in just the first array will remain.
_
.
difference
=
function
(
array
)
{
var
rest
=
concat
.
apply
(
ArrayProto
,
slice
.
call
(
arguments
,
1
));
return
_
.
filter
(
array
,
function
(
value
){
return
!
_
.
contains
(
rest
,
value
);
});
};
// Zip together multiple lists into a single array -- elements that share
// an index go together.
_
.
zip
=
function
()
{
var
args
=
slice
.
call
(
arguments
);
var
length
=
_
.
max
(
_
.
pluck
(
args
,
'
length
'
));
var
results
=
new
Array
(
length
);
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
results
[
i
]
=
_
.
pluck
(
args
,
""
+
i
);
}
return
results
;
};
// Converts lists into objects. Pass either a single array of `[key, value]`
// pairs, or two parallel arrays of the same length -- one of keys, and one of
// the corresponding values.
_
.
object
=
function
(
list
,
values
)
{
if
(
list
==
null
)
return
{};
var
result
=
{};
for
(
var
i
=
0
,
l
=
list
.
length
;
i
<
l
;
i
++
)
{
if
(
values
)
{
result
[
list
[
i
]]
=
values
[
i
];
}
else
{
result
[
list
[
i
][
0
]]
=
list
[
i
][
1
];
}
}
return
result
;
};
// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
// we need this function. Return the position of the first occurrence of an
// item in an array, or -1 if the item is not included in the array.
// Delegates to **ECMAScript 5**'s native `indexOf` if available.
// If the array is large and already in sort order, pass `true`
// for **isSorted** to use binary search.
_
.
indexOf
=
function
(
array
,
item
,
isSorted
)
{
if
(
array
==
null
)
return
-
1
;
var
i
=
0
,
l
=
array
.
length
;
if
(
isSorted
)
{
if
(
typeof
isSorted
==
'
number
'
)
{
i
=
(
isSorted
<
0
?
Math
.
max
(
0
,
l
+
isSorted
)
:
isSorted
);
}
else
{
i
=
_
.
sortedIndex
(
array
,
item
);
return
array
[
i
]
===
item
?
i
:
-
1
;
}
}
if
(
nativeIndexOf
&&
array
.
indexOf
===
nativeIndexOf
)
return
array
.
indexOf
(
item
,
isSorted
);
for
(;
i
<
l
;
i
++
)
if
(
array
[
i
]
===
item
)
return
i
;
return
-
1
;
};
// Delegates to **ECMAScript 5**'s native `lastIndexOf` if available.
_
.
lastIndexOf
=
function
(
array
,
item
,
from
)
{
if
(
array
==
null
)
return
-
1
;
var
hasIndex
=
from
!=
null
;
if
(
nativeLastIndexOf
&&
array
.
lastIndexOf
===
nativeLastIndexOf
)
{
return
hasIndex
?
array
.
lastIndexOf
(
item
,
from
)
:
array
.
lastIndexOf
(
item
);
}
var
i
=
(
hasIndex
?
from
:
array
.
length
);
while
(
i
--
)
if
(
array
[
i
]
===
item
)
return
i
;
return
-
1
;
};
// Generate an integer Array containing an arithmetic progression. A port of
// the native Python `range()` function. See
// [the Python documentation](http://docs.python.org/library/functions.html#range).
_
.
range
=
function
(
start
,
stop
,
step
)
{
if
(
arguments
.
length
<=
1
)
{
stop
=
start
||
0
;
start
=
0
;
}
step
=
arguments
[
2
]
||
1
;
var
len
=
Math
.
max
(
Math
.
ceil
((
stop
-
start
)
/
step
),
0
);
var
idx
=
0
;
var
range
=
new
Array
(
len
);
while
(
idx
<
len
)
{
range
[
idx
++
]
=
start
;
start
+=
step
;
}
return
range
;
};
// Function (ahem) Functions
// ------------------
// Create a function bound to a given object (assigning `this`, and arguments,
// optionally). Delegates to **ECMAScript 5**'s native `Function.bind` if
// available.
_
.
bind
=
function
(
func
,
context
)
{
if
(
func
.
bind
===
nativeBind
&&
nativeBind
)
return
nativeBind
.
apply
(
func
,
slice
.
call
(
arguments
,
1
));
var
args
=
slice
.
call
(
arguments
,
2
);
return
function
()
{
return
func
.
apply
(
context
,
args
.
concat
(
slice
.
call
(
arguments
)));
};
};
// Partially apply a function by creating a version that has had some of its
// arguments pre-filled, without changing its dynamic `this` context.
_
.
partial
=
function
(
func
)
{
var
args
=
slice
.
call
(
arguments
,
1
);
return
function
()
{
return
func
.
apply
(
this
,
args
.
concat
(
slice
.
call
(
arguments
)));
};
};
// Bind all of an object's methods to that object. Useful for ensuring that
// all callbacks defined on an object belong to it.
_
.
bindAll
=
function
(
obj
)
{
var
funcs
=
slice
.
call
(
arguments
,
1
);
if
(
funcs
.
length
===
0
)
funcs
=
_
.
functions
(
obj
);
each
(
funcs
,
function
(
f
)
{
obj
[
f
]
=
_
.
bind
(
obj
[
f
],
obj
);
});
return
obj
;
};
// Memoize an expensive function by storing its results.
_
.
memoize
=
function
(
func
,
hasher
)
{
var
memo
=
{};
hasher
||
(
hasher
=
_
.
identity
);
return
function
()
{
var
key
=
hasher
.
apply
(
this
,
arguments
);
return
_
.
has
(
memo
,
key
)
?
memo
[
key
]
:
(
memo
[
key
]
=
func
.
apply
(
this
,
arguments
));
};
};
// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_
.
delay
=
function
(
func
,
wait
)
{
var
args
=
slice
.
call
(
arguments
,
2
);
return
setTimeout
(
function
(){
return
func
.
apply
(
null
,
args
);
},
wait
);
};
// Defers a function, scheduling it to run after the current call stack has
// cleared.
_
.
defer
=
function
(
func
)
{
return
_
.
delay
.
apply
(
_
,
[
func
,
1
].
concat
(
slice
.
call
(
arguments
,
1
)));
};
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time.
_
.
throttle
=
function
(
func
,
wait
)
{
var
context
,
args
,
timeout
,
result
;
var
previous
=
0
;
var
later
=
function
()
{
previous
=
new
Date
;
timeout
=
null
;
result
=
func
.
apply
(
context
,
args
);
};
return
function
()
{
var
now
=
new
Date
;
var
remaining
=
wait
-
(
now
-
previous
);
context
=
this
;
args
=
arguments
;
if
(
remaining
<=
0
)
{
clearTimeout
(
timeout
);
timeout
=
null
;
previous
=
now
;
result
=
func
.
apply
(
context
,
args
);
}
else
if
(
!
timeout
)
{
timeout
=
setTimeout
(
later
,
remaining
);
}
return
result
;
};
};
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
_
.
debounce
=
function
(
func
,
wait
,
immediate
)
{
var
timeout
,
result
;
return
function
()
{
var
context
=
this
,
args
=
arguments
;
var
later
=
function
()
{
timeout
=
null
;
if
(
!
immediate
)
result
=
func
.
apply
(
context
,
args
);
};
var
callNow
=
immediate
&&
!
timeout
;
clearTimeout
(
timeout
);
timeout
=
setTimeout
(
later
,
wait
);
if
(
callNow
)
result
=
func
.
apply
(
context
,
args
);
return
result
;
};
};
// Returns a function that will be executed at most one time, no matter how
// often you call it. Useful for lazy initialization.
_
.
once
=
function
(
func
)
{
var
ran
=
false
,
memo
;
return
function
()
{
if
(
ran
)
return
memo
;
ran
=
true
;
memo
=
func
.
apply
(
this
,
arguments
);
func
=
null
;
return
memo
;
};
};
// Returns the first function passed as an argument to the second,
// allowing you to adjust arguments, run code before and after, and
// conditionally execute the original function.
_
.
wrap
=
function
(
func
,
wrapper
)
{
return
function
()
{
var
args
=
[
func
];
push
.
apply
(
args
,
arguments
);
return
wrapper
.
apply
(
this
,
args
);
};
};
// Returns a function that is the composition of a list of functions, each
// consuming the return value of the function that follows.
_
.
compose
=
function
()
{
var
funcs
=
arguments
;
return
function
()
{
var
args
=
arguments
;
for
(
var
i
=
funcs
.
length
-
1
;
i
>=
0
;
i
--
)
{
args
=
[
funcs
[
i
].
apply
(
this
,
args
)];
}
return
args
[
0
];
};
};
// Returns a function that will only be executed after being called N times.
_
.
after
=
function
(
times
,
func
)
{
if
(
times
<=
0
)
return
func
();
return
function
()
{
if
(
--
times
<
1
)
{
return
func
.
apply
(
this
,
arguments
);
}
};
};
// Object Functions
// ----------------
// Retrieve the names of an object's properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
_
.
keys
=
nativeKeys
||
function
(
obj
)
{
if
(
obj
!==
Object
(
obj
))
throw
new
TypeError
(
'
Invalid object
'
);
var
keys
=
[];
for
(
var
key
in
obj
)
if
(
_
.
has
(
obj
,
key
))
keys
[
keys
.
length
]
=
key
;
return
keys
;
};
// Retrieve the values of an object's properties.
_
.
values
=
function
(
obj
)
{
var
values
=
[];
for
(
var
key
in
obj
)
if
(
_
.
has
(
obj
,
key
))
values
.
push
(
obj
[
key
]);
return
values
;
};
// Convert an object into a list of `[key, value]` pairs.
_
.
pairs
=
function
(
obj
)
{
var
pairs
=
[];
for
(
var
key
in
obj
)
if
(
_
.
has
(
obj
,
key
))
pairs
.
push
([
key
,
obj
[
key
]]);
return
pairs
;
};
// Invert the keys and values of an object. The values must be serializable.
_
.
invert
=
function
(
obj
)
{
var
result
=
{};
for
(
var
key
in
obj
)
if
(
_
.
has
(
obj
,
key
))
result
[
obj
[
key
]]
=
key
;
return
result
;
};
// Return a sorted list of the function names available on the object.
// Aliased as `methods`
_
.
functions
=
_
.
methods
=
function
(
obj
)
{
var
names
=
[];
for
(
var
key
in
obj
)
{
if
(
_
.
isFunction
(
obj
[
key
]))
names
.
push
(
key
);
}
return
names
.
sort
();
};
// Extend a given object with all the properties in passed-in object(s).
_
.
extend
=
function
(
obj
)
{
each
(
slice
.
call
(
arguments
,
1
),
function
(
source
)
{
if
(
source
)
{
for
(
var
prop
in
source
)
{
obj
[
prop
]
=
source
[
prop
];
}
}
});
return
obj
;
};
// Return a copy of the object only containing the whitelisted properties.
_
.
pick
=
function
(
obj
)
{
var
copy
=
{};
var
keys
=
concat
.
apply
(
ArrayProto
,
slice
.
call
(
arguments
,
1
));
each
(
keys
,
function
(
key
)
{
if
(
key
in
obj
)
copy
[
key
]
=
obj
[
key
];
});
return
copy
;
};
// Return a copy of the object without the blacklisted properties.
_
.
omit
=
function
(
obj
)
{
var
copy
=
{};
var
keys
=
concat
.
apply
(
ArrayProto
,
slice
.
call
(
arguments
,
1
));
for
(
var
key
in
obj
)
{
if
(
!
_
.
contains
(
keys
,
key
))
copy
[
key
]
=
obj
[
key
];
}
return
copy
;
};
// Fill in a given object with default properties.
_
.
defaults
=
function
(
obj
)
{
each
(
slice
.
call
(
arguments
,
1
),
function
(
source
)
{
if
(
source
)
{
for
(
var
prop
in
source
)
{
if
(
obj
[
prop
]
==
null
)
obj
[
prop
]
=
source
[
prop
];
}
}
});
return
obj
;
};
// Create a (shallow-cloned) duplicate of an object.
_
.
clone
=
function
(
obj
)
{
if
(
!
_
.
isObject
(
obj
))
return
obj
;
return
_
.
isArray
(
obj
)
?
obj
.
slice
()
:
_
.
extend
({},
obj
);
};
// Invokes interceptor with the obj, and then returns obj.
// The primary purpose of this method is to "tap into" a method chain, in
// order to perform operations on intermediate results within the chain.
_
.
tap
=
function
(
obj
,
interceptor
)
{
interceptor
(
obj
);
return
obj
;
};
// Internal recursive comparison function for `isEqual`.
var
eq
=
function
(
a
,
b
,
aStack
,
bStack
)
{
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the Harmony `egal` proposal: http://wiki.ecmascript.org/doku.php?id=harmony:egal.
if
(
a
===
b
)
return
a
!==
0
||
1
/
a
==
1
/
b
;
// A strict comparison is necessary because `null == undefined`.
if
(
a
==
null
||
b
==
null
)
return
a
===
b
;
// Unwrap any wrapped objects.
if
(
a
instanceof
_
)
a
=
a
.
_wrapped
;
if
(
b
instanceof
_
)
b
=
b
.
_wrapped
;
// Compare `[[Class]]` names.
var
className
=
toString
.
call
(
a
);
if
(
className
!=
toString
.
call
(
b
))
return
false
;
switch
(
className
)
{
// Strings, numbers, dates, and booleans are compared by value.
case
'
[object String]
'
:
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
// equivalent to `new String("5")`.
return
a
==
String
(
b
);
case
'
[object Number]
'
:
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
// other numeric values.
return
a
!=
+
a
?
b
!=
+
b
:
(
a
==
0
?
1
/
a
==
1
/
b
:
a
==
+
b
);
case
'
[object Date]
'
:
case
'
[object Boolean]
'
:
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return
+
a
==
+
b
;
// RegExps are compared by their source patterns and flags.
case
'
[object RegExp]
'
:
return
a
.
source
==
b
.
source
&&
a
.
global
==
b
.
global
&&
a
.
multiline
==
b
.
multiline
&&
a
.
ignoreCase
==
b
.
ignoreCase
;
}
if
(
typeof
a
!=
'
object
'
||
typeof
b
!=
'
object
'
)
return
false
;
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
var
length
=
aStack
.
length
;
while
(
length
--
)
{
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if
(
aStack
[
length
]
==
a
)
return
bStack
[
length
]
==
b
;
}
// Add the first object to the stack of traversed objects.
aStack
.
push
(
a
);
bStack
.
push
(
b
);
var
size
=
0
,
result
=
true
;
// Recursively compare objects and arrays.
if
(
className
==
'
[object Array]
'
)
{
// Compare array lengths to determine if a deep comparison is necessary.
size
=
a
.
length
;
result
=
size
==
b
.
length
;
if
(
result
)
{
// Deep compare the contents, ignoring non-numeric properties.
while
(
size
--
)
{
if
(
!
(
result
=
eq
(
a
[
size
],
b
[
size
],
aStack
,
bStack
)))
break
;
}
}
}
else
{
// Objects with different constructors are not equivalent, but `Object`s
// from different frames are.
var
aCtor
=
a
.
constructor
,
bCtor
=
b
.
constructor
;
if
(
aCtor
!==
bCtor
&&
!
(
_
.
isFunction
(
aCtor
)
&&
(
aCtor
instanceof
aCtor
)
&&
_
.
isFunction
(
bCtor
)
&&
(
bCtor
instanceof
bCtor
)))
{
return
false
;
}
// Deep compare objects.
for
(
var
key
in
a
)
{
if
(
_
.
has
(
a
,
key
))
{
// Count the expected number of properties.
size
++
;
// Deep compare each member.
if
(
!
(
result
=
_
.
has
(
b
,
key
)
&&
eq
(
a
[
key
],
b
[
key
],
aStack
,
bStack
)))
break
;
}
}
// Ensure that both objects contain the same number of properties.
if
(
result
)
{
for
(
key
in
b
)
{
if
(
_
.
has
(
b
,
key
)
&&
!
(
size
--
))
break
;
}
result
=
!
size
;
}
}
// Remove the first object from the stack of traversed objects.
aStack
.
pop
();
bStack
.
pop
();
return
result
;
};
// Perform a deep comparison to check if two objects are equal.
_
.
isEqual
=
function
(
a
,
b
)
{
return
eq
(
a
,
b
,
[],
[]);
};
// Is a given array, string, or object empty?
// An "empty" object has no enumerable own-properties.
_
.
isEmpty
=
function
(
obj
)
{
if
(
obj
==
null
)
return
true
;
if
(
_
.
isArray
(
obj
)
||
_
.
isString
(
obj
))
return
obj
.
length
===
0
;
for
(
var
key
in
obj
)
if
(
_
.
has
(
obj
,
key
))
return
false
;
return
true
;
};
// Is a given value a DOM element?
_
.
isElement
=
function
(
obj
)
{
return
!!
(
obj
&&
obj
.
nodeType
===
1
);
};
// Is a given value an array?
// Delegates to ECMA5's native Array.isArray
_
.
isArray
=
nativeIsArray
||
function
(
obj
)
{
return
toString
.
call
(
obj
)
==
'
[object Array]
'
;
};
// Is a given variable an object?
_
.
isObject
=
function
(
obj
)
{
return
obj
===
Object
(
obj
);
};
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
each
([
'
Arguments
'
,
'
Function
'
,
'
String
'
,
'
Number
'
,
'
Date
'
,
'
RegExp
'
],
function
(
name
)
{
_
[
'
is
'
+
name
]
=
function
(
obj
)
{
return
toString
.
call
(
obj
)
==
'
[object
'
+
name
+
'
]
'
;
};
});
// Define a fallback version of the method in browsers (ahem, IE), where
// there isn't any inspectable "Arguments" type.
if
(
!
_
.
isArguments
(
arguments
))
{
_
.
isArguments
=
function
(
obj
)
{
return
!!
(
obj
&&
_
.
has
(
obj
,
'
callee
'
));
};
}
// Optimize `isFunction` if appropriate.
if
(
typeof
(
/./
)
!==
'
function
'
)
{
_
.
isFunction
=
function
(
obj
)
{
return
typeof
obj
===
'
function
'
;
};
}
// Is a given object a finite number?
_
.
isFinite
=
function
(
obj
)
{
return
isFinite
(
obj
)
&&
!
isNaN
(
parseFloat
(
obj
));
};
// Is the given value `NaN`? (NaN is the only number which does not equal itself).
_
.
isNaN
=
function
(
obj
)
{
return
_
.
isNumber
(
obj
)
&&
obj
!=
+
obj
;
};
// Is a given value a boolean?
_
.
isBoolean
=
function
(
obj
)
{
return
obj
===
true
||
obj
===
false
||
toString
.
call
(
obj
)
==
'
[object Boolean]
'
;
};
// Is a given value equal to null?
_
.
isNull
=
function
(
obj
)
{
return
obj
===
null
;
};
// Is a given variable undefined?
_
.
isUndefined
=
function
(
obj
)
{
return
obj
===
void
0
;
};
// Shortcut function for checking if an object has a given property directly
// on itself (in other words, not on a prototype).
_
.
has
=
function
(
obj
,
key
)
{
return
hasOwnProperty
.
call
(
obj
,
key
);
};
// Utility Functions
// -----------------
// Run Underscore.js in *noConflict* mode, returning the `_` variable to its
// previous owner. Returns a reference to the Underscore object.
_
.
noConflict
=
function
()
{
root
.
_
=
previousUnderscore
;
return
this
;
};
// Keep the identity function around for default iterators.
_
.
identity
=
function
(
value
)
{
return
value
;
};
// Run a function **n** times.
_
.
times
=
function
(
n
,
iterator
,
context
)
{
var
accum
=
Array
(
n
);
for
(
var
i
=
0
;
i
<
n
;
i
++
)
accum
[
i
]
=
iterator
.
call
(
context
,
i
);
return
accum
;
};
// Return a random integer between min and max (inclusive).
_
.
random
=
function
(
min
,
max
)
{
if
(
max
==
null
)
{
max
=
min
;
min
=
0
;
}
return
min
+
Math
.
floor
(
Math
.
random
()
*
(
max
-
min
+
1
));
};
// List of HTML entities for escaping.
var
entityMap
=
{
escape
:
{
'
&
'
:
'
&
'
,
'
<
'
:
'
<
'
,
'
>
'
:
'
>
'
,
'
"
'
:
'
"
'
,
"
'
"
:
'
'
'
,
'
/
'
:
'
/
'
}
};
entityMap
.
unescape
=
_
.
invert
(
entityMap
.
escape
);
// Regexes containing the keys and values listed immediately above.
var
entityRegexes
=
{
escape
:
new
RegExp
(
'
[
'
+
_
.
keys
(
entityMap
.
escape
).
join
(
''
)
+
'
]
'
,
'
g
'
),
unescape
:
new
RegExp
(
'
(
'
+
_
.
keys
(
entityMap
.
unescape
).
join
(
'
|
'
)
+
'
)
'
,
'
g
'
)
};
// Functions for escaping and unescaping strings to/from HTML interpolation.
_
.
each
([
'
escape
'
,
'
unescape
'
],
function
(
method
)
{
_
[
method
]
=
function
(
string
)
{
if
(
string
==
null
)
return
''
;
return
(
''
+
string
).
replace
(
entityRegexes
[
method
],
function
(
match
)
{
return
entityMap
[
method
][
match
];
});
};
});
// If the value of the named property is a function then invoke it;
// otherwise, return it.
_
.
result
=
function
(
object
,
property
)
{
if
(
object
==
null
)
return
null
;
var
value
=
object
[
property
];
return
_
.
isFunction
(
value
)
?
value
.
call
(
object
)
:
value
;
};
// Add your own custom functions to the Underscore object.
_
.
mixin
=
function
(
obj
)
{
each
(
_
.
functions
(
obj
),
function
(
name
){
var
func
=
_
[
name
]
=
obj
[
name
];
_
.
prototype
[
name
]
=
function
()
{
var
args
=
[
this
.
_wrapped
];
push
.
apply
(
args
,
arguments
);
return
result
.
call
(
this
,
func
.
apply
(
_
,
args
));
};
});
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
var
idCounter
=
0
;
_
.
uniqueId
=
function
(
prefix
)
{
var
id
=
++
idCounter
+
''
;
return
prefix
?
prefix
+
id
:
id
;
};
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
_
.
templateSettings
=
{
evaluate
:
/<%
([\s\S]
+
?)
%>/g
,
interpolate
:
/<%=
([\s\S]
+
?)
%>/g
,
escape
:
/<%-
([\s\S]
+
?)
%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var
noMatch
=
/
(
.
)
^/
;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var
escapes
=
{
"
'
"
:
"
'
"
,
'
\\
'
:
'
\\
'
,
'
\r
'
:
'
r
'
,
'
\n
'
:
'
n
'
,
'
\t
'
:
'
t
'
,
'
\
u2028
'
:
'
u2028
'
,
'
\
u2029
'
:
'
u2029
'
};
var
escaper
=
/
\\
|'|
\r
|
\n
|
\t
|
\u
2028|
\u
2029/g
;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_
.
template
=
function
(
text
,
data
,
settings
)
{
var
render
;
settings
=
_
.
defaults
({},
settings
,
_
.
templateSettings
);
// Combine delimiters into one regular expression via alternation.
var
matcher
=
new
RegExp
([
(
settings
.
escape
||
noMatch
).
source
,
(
settings
.
interpolate
||
noMatch
).
source
,
(
settings
.
evaluate
||
noMatch
).
source
].
join
(
'
|
'
)
+
'
|$
'
,
'
g
'
);
// Compile the template source, escaping string literals appropriately.
var
index
=
0
;
var
source
=
"
__p+='
"
;
text
.
replace
(
matcher
,
function
(
match
,
escape
,
interpolate
,
evaluate
,
offset
)
{
source
+=
text
.
slice
(
index
,
offset
)
.
replace
(
escaper
,
function
(
match
)
{
return
'
\\
'
+
escapes
[
match
];
});
if
(
escape
)
{
source
+=
"
'+
\n
((__t=(
"
+
escape
+
"
))==null?'':_.escape(__t))+
\n
'
"
;
}
if
(
interpolate
)
{
source
+=
"
'+
\n
((__t=(
"
+
interpolate
+
"
))==null?'':__t)+
\n
'
"
;
}
if
(
evaluate
)
{
source
+=
"
';
\n
"
+
evaluate
+
"
\n
__p+='
"
;
}
index
=
offset
+
match
.
length
;
return
match
;
});
source
+=
"
';
\n
"
;
// If a variable is not specified, place data values in local scope.
if
(
!
settings
.
variable
)
source
=
'
with(obj||{}){
\n
'
+
source
+
'
}
\n
'
;
source
=
"
var __t,__p='',__j=Array.prototype.join,
"
+
"
print=function(){__p+=__j.call(arguments,'');};
\n
"
+
source
+
"
return __p;
\n
"
;
try
{
render
=
new
Function
(
settings
.
variable
||
'
obj
'
,
'
_
'
,
source
);
}
catch
(
e
)
{
e
.
source
=
source
;
throw
e
;
}
if
(
data
)
return
render
(
data
,
_
);
var
template
=
function
(
data
)
{
return
render
.
call
(
this
,
data
,
_
);
};
// Provide the compiled function source as a convenience for precompilation.
template
.
source
=
'
function(
'
+
(
settings
.
variable
||
'
obj
'
)
+
'
){
\n
'
+
source
+
'
}
'
;
return
template
;
};
// Add a "chain" function, which will delegate to the wrapper.
_
.
chain
=
function
(
obj
)
{
return
_
(
obj
).
chain
();
};
// OOP
// ---------------
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
// Helper function to continue chaining intermediate results.
var
result
=
function
(
obj
)
{
return
this
.
_chain
?
_
(
obj
).
chain
()
:
obj
;
};
// Add all of the Underscore functions to the wrapper object.
_
.
mixin
(
_
);
// Add all mutator Array functions to the wrapper.
each
([
'
pop
'
,
'
push
'
,
'
reverse
'
,
'
shift
'
,
'
sort
'
,
'
splice
'
,
'
unshift
'
],
function
(
name
)
{
var
method
=
ArrayProto
[
name
];
_
.
prototype
[
name
]
=
function
()
{
var
obj
=
this
.
_wrapped
;
method
.
apply
(
obj
,
arguments
);
if
((
name
==
'
shift
'
||
name
==
'
splice
'
)
&&
obj
.
length
===
0
)
delete
obj
[
0
];
return
result
.
call
(
this
,
obj
);
};
});
// Add all accessor Array functions to the wrapper.
each
([
'
concat
'
,
'
join
'
,
'
slice
'
],
function
(
name
)
{
var
method
=
ArrayProto
[
name
];
_
.
prototype
[
name
]
=
function
()
{
return
result
.
call
(
this
,
method
.
apply
(
this
.
_wrapped
,
arguments
));
};
});
_
.
extend
(
_
.
prototype
,
{
// Start chaining a wrapped Underscore object.
chain
:
function
()
{
this
.
_chain
=
true
;
return
this
;
},
// Extracts the result from a wrapped and chained object.
value
:
function
()
{
return
this
.
_wrapped
;
}
});
}).
call
(
this
);
dependency-examples/backbone_require/index.html
View file @
d501a8f5
...
...
@@ -6,7 +6,6 @@
<title>
Backbone.js + RequireJS • TodoMVC
</title>
<link
rel=
"stylesheet"
href=
"../../assets/base.css"
>
<script
src=
"../../assets/base.js"
></script>
<script
data-main=
"js/main"
src=
"js/lib/require/require.js"
></script>
<!--[if IE]>
<script src="../../assets/ie.js"></script>
<![endif]-->
...
...
@@ -29,5 +28,6 @@
<p>
Written by
<a
href=
"http://addyosmani.github.com/todomvc/"
>
Addy Osmani
</a></p>
<p>
Part of
<a
href=
"http://todomvc.com"
>
TodoMVC
</a></p>
</footer>
<script
data-main=
"js/main"
src=
"components/requirejs/require.js"
></script>
</body>
</html>
dependency-examples/backbone_require/js/main.js
View file @
d501a8f5
...
...
@@ -22,11 +22,11 @@ require.config({
}
},
paths
:
{
jquery
:
'
../
../../assets/jquery.min
'
,
underscore
:
'
../
../../assets/lodash.min
'
,
backbone
:
'
lib
/backbone/backbone
'
,
backboneLocalstorage
:
'
lib/backbon
e/backbone.localStorage
'
,
text
:
'
lib/require
/text
'
jquery
:
'
../
components/jquery/jquery
'
,
underscore
:
'
../
components/underscore/underscore
'
,
backbone
:
'
../components
/backbone/backbone
'
,
backboneLocalstorage
:
'
../components/backbone.localStorag
e/backbone.localStorage
'
,
text
:
'
../components/requirejs-text
/text
'
}
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment