@ -1,11 +1,36 @@
( function ( root , define , require , exports , module , factory , undef ) {
/ * ! B a c k b o n e . M u t a t o r s - v 0 . 4 . 0
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Build @ 2013 - 05 - 01
Documentation and Full License Available at :
http : //asciidisco.github.com/Backbone.Mutators/index.html
git : //github.com/asciidisco/Backbone.Mutators.git
Copyright ( c ) 2013 Sebastian Golasch < public @ asciidisco . com >
Permission is hereby granted , free of charge , to any person obtaining a
copy of this software and associated documentation files ( the "Software" ) ,
to deal in the Software without restriction , including without limitation
the rights to use , copy , modify , merge , publish , distribute , sublicense ,
and / or sell copies of the Software , and to permit persons to whom the
Software is furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND ,
EXPRESS OR IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT .
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM ,
DAMAGES OR OTHER LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE ,
ARISING FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE . * /
( function ( root , factory , undef ) {
'use strict' ;
'use strict' ;
if ( typeof exports === 'object' ) {
if ( typeof exports === 'object' ) {
// Node. Does not work with strict CommonJS, but
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// only CommonJS-like enviroments that support module.exports,
// like Node.
// like Node.
module . exports = factory ( require ( 'underscore' ) , require ( 'backbone' ) ) ;
module . exports = factory ( require ( 'underscore' ) , require ( ' B ackbone') ) ;
} else if ( typeof define === 'function' && define . amd ) {
} else if ( typeof define === 'function' && define . amd ) {
// AMD. Register as an anonymous module.
// AMD. Register as an anonymous module.
define ( [ 'underscore' , 'backbone' ] , function ( _ , Backbone ) {
define ( [ 'underscore' , 'backbone' ] , function ( _ , Backbone ) {
@ -57,7 +82,7 @@
// user.get('fullname') // returns 'Sebastian Golasch'
// user.get('fullname') // returns 'Sebastian Golasch'
// user.toJSON() // return '{firstname: 'Sebastian', lastname: 'Golasch', fullname: 'Sebastian Golasch'}'
// user.toJSON() // return '{firstname: 'Sebastian', lastname: 'Golasch', fullname: 'Sebastian Golasch'}'
} ( this , this . define , this . require , this . exports , this . module , function ( _ , Backbone , root , undef ) {
} ( this , function ( _ , Backbone , root , undef ) {
'use strict' ;
'use strict' ;
// check if we use the amd branch of backbone and underscore
// check if we use the amd branch of backbone and underscore
@ -94,8 +119,7 @@
Mutator . prototype . set = function ( key , value , options ) {
Mutator . prototype . set = function ( key , value , options ) {
var isMutator = this . mutators !== undef ,
var isMutator = this . mutators !== undef ,
ret = null ,
ret = null ,
attrs = null ,
attrs = null ;
attr = null ;
// seamleassly stolen from backbone core
// seamleassly stolen from backbone core
// check if the setter action is triggered
// check if the setter action is triggered
@ -121,6 +145,7 @@
if ( _ . isObject ( attrs ) ) {
if ( _ . isObject ( attrs ) ) {
_ . each ( attrs , _ . bind ( function ( attr , attrKey ) {
_ . each ( attrs , _ . bind ( function ( attr , attrKey ) {
var cur _ret = null ;
if ( isMutator === true && _ . isObject ( this . mutators [ attrKey ] ) === true ) {
if ( isMutator === true && _ . isObject ( this . mutators [ attrKey ] ) === true ) {
// check if we need to set a single value
// check if we need to set a single value
@ -133,10 +158,16 @@
if ( options === undef || ( _ . isObject ( options ) === true && options . silent !== true && ( options . mutators !== undef && options . mutators . silent !== true ) ) ) {
if ( options === undef || ( _ . isObject ( options ) === true && options . silent !== true && ( options . mutators !== undef && options . mutators . silent !== true ) ) ) {
this . trigger ( 'mutators:set:' + attrKey ) ;
this . trigger ( 'mutators:set:' + attrKey ) ;
}
}
ret = meth . call ( this , attrKey , attr , options , _ . bind ( oldSet , this ) ) ;
cur_ ret = meth . call ( this , attrKey , attr , options , _ . bind ( oldSet , this ) ) ;
}
}
}
}
if ( cur _ret === null ) {
cur _ret = _ . bind ( oldSet , this ) ( attrKey , attr , options ) ;
}
if ( ret !== false ) { ret = cur _ret ; }
} , this ) ) ;
} , this ) ) ;
}
}
@ -154,7 +185,7 @@
var attr = oldToJson . call ( this ) ;
var attr = oldToJson . call ( this ) ;
// iterate over all mutators (if there are some)
// iterate over all mutators (if there are some)
_ . each ( this . mutators , _ . bind ( function ( mutator , name ) {
_ . each ( this . mutators , _ . bind ( function ( mutator , name ) {
// check if we have some getter mutations (nested or )
// check if we have some getter mutations
if ( _ . isObject ( this . mutators [ name ] ) === true && _ . isFunction ( this . mutators [ name ] . get ) ) {
if ( _ . isObject ( this . mutators [ name ] ) === true && _ . isFunction ( this . mutators [ name ] . get ) ) {
attr [ name ] = _ . bind ( this . mutators [ name ] . get , this ) ( ) ;
attr [ name ] = _ . bind ( this . mutators [ name ] . get , this ) ( ) ;
} else {
} else {
@ -165,6 +196,12 @@
return attr ;
return attr ;
} ;
} ;
// override get functionality to get HTML-escaped the mutator props
Mutator . prototype . escape = function ( attr ) {
var val = this . get ( attr ) ;
return _ . escape ( val == null ? '' : '' + val ) ;
} ;
// extend the models prototype
// extend the models prototype
_ . extend ( Backbone . Model . prototype , Mutator . prototype ) ;
_ . extend ( Backbone . Model . prototype , Mutator . prototype ) ;