Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

Backbone.Mutators

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Backbone.Mutators - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

.editorconfig

72

backbone.mutators.js

@@ -1,8 +0,8 @@

/*! Backbone.Mutators - v0.3.0
/*! Backbone.Mutators - v0.4.0
------------------------------
Build @ 2012-05-19
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) 2012 Sebastian Golasch <public@asciidisco.com>
Copyright (c) 2013 Sebastian Golasch <public@asciidisco.com>

@@ -26,6 +26,5 @@ Permission is hereby granted, free of charge, to any person obtaining a

IN THE SOFTWARE.*/
(function (root, factory, undef) {
'use strict';
(function (root, define, require, exports, module, factory, undef) {
'use strict';
if (typeof exports === 'object') {

@@ -35,3 +34,3 @@ // Node. Does not work with strict CommonJS, but

// like Node.
module.exports = factory(require('underscore'), require('backbone'));
module.exports = factory(require('underscore'), require('Backbone'));
} else if (typeof define === 'function' && define.amd) {

@@ -88,3 +87,3 @@ // AMD. Register as an anonymous module.

}(this, this.define, this.require, this.exports, this.module, function (_, Backbone, root, undef) {
}(this, function (_, Backbone, root, undef) {
'use strict';

@@ -102,2 +101,5 @@

// This is necessary to ensure that Models declared without the mutators object do not throw and error
Mutator.prototype.mutators = {};
// override get functionality to fetch the mutator props

@@ -109,3 +111,3 @@ Mutator.prototype.get = function (attr) {

if (isMutator === true && _.isFunction(this.mutators[attr]) === true) {
return _.bind(this.mutators[attr], this.attributes)();
return this.mutators[attr].call(this);
}

@@ -115,3 +117,3 @@

if (isMutator === true && _.isObject(this.mutators[attr]) === true && _.isFunction(this.mutators[attr].get) === true) {
return _.bind(this.mutators[attr].get, this.attributes)();
return this.mutators[attr].get.call(this);
}

@@ -124,10 +126,9 @@

Mutator.prototype.set = function (key, value, options) {
var isMutator = this.mutators !== undef,
ret = null,
attrs = null,
attr = null;
var isMutator = this.mutators !== undef,
ret = null,
attrs = null;
// seamleassly stolen from backbone core
// check if the setter action is triggered
// using key <-> value or object
// check if the setter action is triggered
// using key <-> value or object
if (_.isObject(key) || key === null) {

@@ -146,20 +147,37 @@ attrs = key;

if (_.isFunction(this.mutators[key].set) === true) {
ret = _.bind(this.mutators[key].set, this)(key, attrs[key], options, _.bind(oldSet, this));
ret = this.mutators[key].set.call(this, key, attrs[key], options, _.bind(oldSet, this));
} else if(_.isFunction(this.mutators[key])){
ret = this.mutators[key].call(this, key, attrs[key], options, _.bind(oldSet, this));
}
}
if (value === undef && options === undef) {
if (_.isObject(attrs)) {
_.each(attrs, _.bind(function (attr, attrKey) {
var cur_ret = null;
if (isMutator === true && _.isObject(this.mutators[attrKey]) === true) {
// check if we need to set a single value
if (_.isFunction(this.mutators[attrKey].set) === true) {
var meth = this.mutators[attrKey];
if(_.isFunction(meth.set)){
meth = meth.set;
}
if(_.isFunction(meth)){
if (options === undef || (_.isObject(options) === true && options.silent !== true && (options.mutators !== undef && options.mutators.silent !== true))) {
this.trigger('mutators:set:' + attrKey);
}
ret = _.bind(this.mutators[attrKey].set, 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));
}
//validation purposes
if (ret !== null) {

@@ -178,7 +196,7 @@ return ret;

_.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)) {
attr[name] = _.bind(this.mutators[name].get, this.attributes)();
attr[name] = _.bind(this.mutators[name].get, this)();
} else {
attr[name] = _.bind(this.mutators[name], this.attributes)();
attr[name] = _.bind(this.mutators[name], this)();
}

@@ -190,2 +208,8 @@ }, this));

// 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

@@ -197,2 +221,2 @@ _.extend(Backbone.Model.prototype, Mutator.prototype);

return Mutator;
}));
}));

@@ -1,8 +0,8 @@

/*! Backbone.Mutators - v0.3.0
/*! Backbone.Mutators - v0.4.0
------------------------------
Build @ 2012-05-19
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) 2012 Sebastian Golasch <public@asciidisco.com>
Copyright (c) 2013 Sebastian Golasch <public@asciidisco.com>

@@ -26,2 +26,2 @@ Permission is hereby granted, free of charge, to any person obtaining a

IN THE SOFTWARE.*/
(function(a,b,c,d,e,f,g){"use strict",typeof d=="object"?e.exports=f(c("underscore"),c("backbone")):typeof b=="function"&&b.amd?b(["underscore","backbone"],function(b,c){return b=b===g?a._:b,c=c===g?a.Backbone:c,a.returnExportsGlobal=f(b,c,a)}):a.returnExportsGlobal=f(a._,a.Backbone)})(this,this.define,this.require,this.exports,this.module,function(a,b,c,d){"use strict",b=b===d?c.Backbone:b,a=a===d?c._:a;var e=function(){},f=b.Model.prototype.get,g=b.Model.prototype.set,h=b.Model.prototype.toJSON;return e.prototype.get=function(b){var c=this.mutators!==d;return c===!0&&a.isFunction(this.mutators[b])===!0?a.bind(this.mutators[b],this.attributes)():c===!0&&a.isObject(this.mutators[b])===!0&&a.isFunction(this.mutators[b].get)===!0?a.bind(this.mutators[b].get,this.attributes)():f.call(this,b)},e.prototype.set=function(b,c,e){var f=this.mutators!==d,h=null,i=null,j=null;return a.isObject(b)||b===null?(i=b,e=c):(i={},i[b]=c),f===!0&&a.isObject(this.mutators[b])===!0&&a.isFunction(this.mutators[b].set)===!0&&(h=a.bind(this.mutators[b].set,this)(b,i[b],e,a.bind(g,this))),c===d&&e===d&&a.each(i,a.bind(function(b,c){f===!0&&a.isObject(this.mutators[c])===!0&&a.isFunction(this.mutators[c].set)===!0&&((e===d||a.isObject(e)===!0&&e.silent!==!0&&e.mutators!==d&&e.mutators.silent!==!0)&&this.trigger("mutators:set:"+c),h=a.bind(this.mutators[c].set,this)(c,b,e,a.bind(g,this)))},this)),h!==null?h:g.call(this,b,c,e)},e.prototype.toJSON=function(){var b=h.call(this);return a.each(this.mutators,a.bind(function(c,d){a.isObject(this.mutators[d])===!0&&a.isFunction(this.mutators[d].get)?b[d]=a.bind(this.mutators[d].get,this.attributes)():b[d]=a.bind(this.mutators[d],this.attributes)()},this)),b},a.extend(b.Model.prototype,e.prototype),b.Mutators=e,e});
(function(t,s,i){"use strict";"object"==typeof exports?module.exports=s(require("underscore"),require("Backbone")):"function"==typeof define&&define.amd?define(["underscore","backbone"],function(o,e){return o=o===i?t._:o,e=e===i?t.Backbone:e,t.returnExportsGlobal=s(o,e,t)}):t.returnExportsGlobal=s(t._,t.Backbone)})(this,function(t,s,i,o){"use strict";s=s===o?i.Backbone:s,t=t===o?i._:t;var e=function(){},r=s.Model.prototype.get,n=s.Model.prototype.set,u=s.Model.prototype.toJSON;return e.prototype.mutators={},e.prototype.get=function(s){var i=this.mutators!==o;return i===!0&&t.isFunction(this.mutators[s])===!0?this.mutators[s].call(this):i===!0&&t.isObject(this.mutators[s])===!0&&t.isFunction(this.mutators[s].get)===!0?this.mutators[s].get.call(this):r.call(this,s)},e.prototype.set=function(s,i,e){var r=this.mutators!==o,u=null,a=null;return t.isObject(s)||null===s?(a=s,e=i):(a={},a[s]=i),r===!0&&t.isObject(this.mutators[s])===!0&&(t.isFunction(this.mutators[s].set)===!0?u=this.mutators[s].set.call(this,s,a[s],e,t.bind(n,this)):t.isFunction(this.mutators[s])&&(u=this.mutators[s].call(this,s,a[s],e,t.bind(n,this)))),t.isObject(a)&&t.each(a,t.bind(function(s,i){var a=null;if(r===!0&&t.isObject(this.mutators[i])===!0){var c=this.mutators[i];t.isFunction(c.set)&&(c=c.set),t.isFunction(c)&&((e===o||t.isObject(e)===!0&&e.silent!==!0&&e.mutators!==o&&e.mutators.silent!==!0)&&this.trigger("mutators:set:"+i),a=c.call(this,i,s,e,t.bind(n,this)))}null===a&&(a=t.bind(n,this)(i,s,e)),u!==!1&&(u=a)},this)),null!==u?u:n.call(this,s,i,e)},e.prototype.toJSON=function(){var s=u.call(this);return t.each(this.mutators,t.bind(function(i,o){s[o]=t.isObject(this.mutators[o])===!0&&t.isFunction(this.mutators[o].get)?t.bind(this.mutators[o].get,this)():t.bind(this.mutators[o],this)()},this)),s},e.prototype.escape=function(s){var i=this.get(s);return t.escape(null==i?"":""+i)},t.extend(s.Model.prototype,e.prototype),s.Mutators=e,e});
{
"name": "Backbone.Mutators",
"version": "0.3.0",
"description": "Backbone plugin to override getters and setters with logic",
"homepage": "http://asciidisco.github.com/Backbone.Mutators/index.html",
"keywords": ["backbone", "plugin", "mutator", "getter", "setter", "client", "browser"],
"repository": {"type": "git", "url": "git://github.com/asciidisco/Backbone.Mutators.git"},
"author" : "Sebastian Golasch <public@asciidisco.com>",
"maintainers": [
{
"name": "Sebastian Golasch (asciidisco)",
"email": "public@asciidisco.com",
"web": "http://asciidisco.com"
}
],
"licenses": [{ "type": "MIT", "url": "http://mit-license.org/" }],
"repositories": [{ "type": "git", "url": "https://github.com/asciidisco/Backbone.Mutators" }],
"lib": ".",
"main": "backbone.mutators.js",
"devDependencies": {
"grunt": "~0.3.x"
},
"dependencies" : {
"underscore" : ">=1.3.1",
"backbone": ">=0.9.x"
"name": "Backbone.Mutators",
"version": "0.4.0",
"description": "Backbone plugin to override getters and setters with logic",
"homepage": "http://asciidisco.github.com/Backbone.Mutators/index.html",
"keywords": [
"backbone",
"plugin",
"mutator",
"getter",
"setter",
"client",
"browser"
],
"repository": {
"type": "git",
"url": "git://github.com/asciidisco/Backbone.Mutators.git"
},
"author": "Sebastian Golasch <public@asciidisco.com>",
"maintainers": [
{
"name": "Sebastian Golasch (asciidisco)",
"email": "public@asciidisco.com",
"web": "http://asciidisco.com"
}
],
"licenses": [
{
"type": "MIT",
"url": "http://mit-license.org/"
}
],
"repositories": [
{
"type": "git",
"url": "https://github.com/asciidisco/Backbone.Mutators"
}
],
"lib": ".",
"main": "backbone.mutators.js",
"devDependencies": {
"qunitjs": "~1.11.x",
"grunt": "~0.4.x",
"grunt-qunit-istanbul": "~0.1.x",
"grunt-contrib-jshint": "~0.2.x",
"grunt-contrib-uglify": "~0.2.x",
"grunt-contrib-concat": "~0.3.x",
"grunt-contrib-nodeunit": "~0.1.x",
"grunt-contrib-yuidoc": "~0.4.x",
"grunt-saucelabs": "~4.0.0",
"grunt-docco": "~0.2.x",
"grunt-plato": "~0.2.x",
"grunt-complexity": "~0.1.x"
},
"dependencies": {
"underscore": "~1.4.x",
"backbone": "~1.0.x"
}
}
}

@@ -5,3 +5,4 @@ ## Backbone.Mutators

## Build Status, Project Page, Annotated Source & Tests
[![Build Status](https://secure.travis-ci.org/asciidisco/Backbone.Mutators.png?branch=master)](http://travis-ci.org/asciidisco/Backbone.Mutators)<br /><br />
[![Build Status](https://secure.travis-ci.org/asciidisco/Backbone.Mutators.png?branch=master)](http://travis-ci.org/asciidisco/Backbone.Mutators)
[![Unit Test Status](https://saucelabs.com/buildstatus/asciidisco)](https://saucelabs.com/u/asciidisco)<br /><br />
[Project Page](http://asciidisco.github.com/Backbone.Mutators/index.html)<br />

@@ -25,3 +26,3 @@ [Docs](http://asciidisco.github.com/Backbone.Mutators/docs/backbone.mutators.html)<br />

### Dowload
You can directly download the
You can directly download the
[Development Version](https://raw.github.com/asciidisco/Backbone.Mutators/master/backbone.mutators.js)

@@ -80,3 +81,3 @@ or the

fullname: function () {
return this.firstname + ' ' + this.lastname;
return this.get('firstname') + ' ' + this.get('lastname');
}

@@ -91,3 +92,3 @@ },

var user = new User();
// use get to get the 'mutated' value
// use get to get the 'mutated' value
user.get('fullname') // 'Sugar Daddy'

@@ -104,3 +105,3 @@ // serialize the model and see the 'mutated' value in the resulting JSON

status: function () {
return this.status === true ? 'Workish' : 'Bad bad error';
return this.get('status') === true ? 'Workish' : 'Bad bad error';
}

@@ -114,3 +115,3 @@ },

var state = new State();
// use get to get the 'mutated' value
// use get to get the 'mutated' value
state.get('status') // 'Workish'

@@ -133,3 +134,3 @@ // serialize the model and see the 'mutated' value in the resulting JSON

get: function () {
return this.firstname + ' ' + this.lastname;
return this.get('firstname') + ' ' + this.get('lastname');
}

@@ -145,3 +146,3 @@ }

var user = new User();
// use get to get the 'mutated' value
// use get to get the 'mutated' value
user.set('fullname', 'Big Mama', {silent: true});

@@ -166,3 +167,3 @@ // serialize the model and see the 'mutated' value in the resulting JSON

get: function () {
return this.firstname + ' ' + this.lastname;
return this.get('firstname') + ' ' + this.get('lastname');
}

@@ -194,5 +195,5 @@ }

// use get to get the 'mutated' value
// use get to get the 'mutated' value
user.set('fullname', 'Big Mama');
// serialize the model and see the 'mutated' value in the resulting JSON

@@ -216,3 +217,3 @@ user.get('fullname') // 'Big Mama'

get: function () {
return this.firstname + ' ' + this.lastname;
return this.get('firstname') + ' ' + this.get('lastname');
}

@@ -247,5 +248,5 @@ }

// use get to get the 'mutated' value
// use get to get the 'mutated' value
user.set('fullname', 'Big Mama', {mutators: {silence: true}});
// serialize the model and see the 'mutated' value in the resulting JSON

@@ -262,3 +263,3 @@ user.get('fullname') // 'Big Mama'

mutators: {
fullname: {
iAcceptOnlyLowercaseStuff: {
set: function (key, value, options, set) {

@@ -276,3 +277,3 @@ // call the original setter with the lowercased value

var spicy = new Spicy();
// use get to get the 'mutated' value
// use get to get the 'mutated' value
spicy.set('iAcceptOnlyLowercaseStuff', 'SALT');

@@ -282,2 +283,24 @@ spicy.get('iAcceptOnlyLowercaseStuff') // 'salt'

### Define one getter / setter method
```javascript
var User = Backbone.Model.extend({
// Define mutator properties
mutators: {
fullname: function (key, value, options, set) {
if(key){
var names = value.split(' ');
this.set('firstname', names[0], options);
this.set('lastname', names[1], options);
}
return this.get('firstname') + ' ' + this.get('lastname');
}
},
defaults: {
firstname: 'Sugar',
lastname: 'Daddy'
}
});
```
### Define multiple mutators

@@ -295,3 +318,3 @@ ```javascript

get: function () {
return this.firstname + ' ' + this.lastname;
return this.get('firstname') + ' ' + this.get('lastname');
}

@@ -316,2 +339,6 @@ },

### 0.3.1
+ Change get context to modal instead of attributes
+ Added single getter / setter method
### 0.3.0

@@ -334,2 +361,2 @@ + Removed the Cake based build process and moved to grunt

### 0.1.0
+ Initial Release
+ Initial Release

@@ -1,4 +0,4 @@

(function (root, define, require, exports, module, factory, undef) {
(function (root, factory, undef) {
'use strict';
if (typeof exports === 'object') {

@@ -8,3 +8,3 @@ // Node. Does not work with strict CommonJS, but

// like Node.
module.exports = factory(require('underscore'), require('backbone'));
module.exports = factory(require('underscore'), require('Backbone'));
} else if (typeof define === 'function' && define.amd) {

@@ -61,3 +61,3 @@ // AMD. Register as an anonymous module.

}(this, this.define, this.require, this.exports, this.module, function (_, Backbone, root, undef) {
}(this, function (_, Backbone, root, undef) {
'use strict';

@@ -75,2 +75,5 @@

// This is necessary to ensure that Models declared without the mutators object do not throw and error
Mutator.prototype.mutators = {};
// override get functionality to fetch the mutator props

@@ -82,3 +85,3 @@ Mutator.prototype.get = function (attr) {

if (isMutator === true && _.isFunction(this.mutators[attr]) === true) {
return _.bind(this.mutators[attr], this.attributes)();
return this.mutators[attr].call(this);
}

@@ -88,3 +91,3 @@

if (isMutator === true && _.isObject(this.mutators[attr]) === true && _.isFunction(this.mutators[attr].get) === true) {
return _.bind(this.mutators[attr].get, this.attributes)();
return this.mutators[attr].get.call(this);
}

@@ -97,10 +100,9 @@

Mutator.prototype.set = function (key, value, options) {
var isMutator = this.mutators !== undef,
ret = null,
attrs = null,
attr = null;
var isMutator = this.mutators !== undef,
ret = null,
attrs = null;
// seamleassly stolen from backbone core
// check if the setter action is triggered
// using key <-> value or object
// check if the setter action is triggered
// using key <-> value or object
if (_.isObject(key) || key === null) {

@@ -119,20 +121,37 @@ attrs = key;

if (_.isFunction(this.mutators[key].set) === true) {
ret = _.bind(this.mutators[key].set, this)(key, attrs[key], options, _.bind(oldSet, this));
ret = this.mutators[key].set.call(this, key, attrs[key], options, _.bind(oldSet, this));
} else if(_.isFunction(this.mutators[key])){
ret = this.mutators[key].call(this, key, attrs[key], options, _.bind(oldSet, this));
}
}
if (value === undef && options === undef) {
if (_.isObject(attrs)) {
_.each(attrs, _.bind(function (attr, attrKey) {
var cur_ret = null;
if (isMutator === true && _.isObject(this.mutators[attrKey]) === true) {
// check if we need to set a single value
if (_.isFunction(this.mutators[attrKey].set) === true) {
var meth = this.mutators[attrKey];
if(_.isFunction(meth.set)){
meth = meth.set;
}
if(_.isFunction(meth)){
if (options === undef || (_.isObject(options) === true && options.silent !== true && (options.mutators !== undef && options.mutators.silent !== true))) {
this.trigger('mutators:set:' + attrKey);
}
ret = _.bind(this.mutators[attrKey].set, 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));
}
//validation purposes
if (ret !== null) {

@@ -151,7 +170,7 @@ return ret;

_.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)) {
attr[name] = _.bind(this.mutators[name].get, this.attributes)();
attr[name] = _.bind(this.mutators[name].get, this)();
} else {
attr[name] = _.bind(this.mutators[name], this.attributes)();
attr[name] = _.bind(this.mutators[name], this)();
}

@@ -163,2 +182,8 @@ }, this));

// 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

@@ -170,2 +195,2 @@ _.extend(Backbone.Model.prototype, Mutator.prototype);

return Mutator;
}));
}));
module('Backbone.Mutators');
test("can get 'normal' value", function () {
expect(2);
var Model = Backbone.Model.extend({
mutators: {
fullname: function (attributes) {
return this.firstname + ' ' + this.lastname;
}
}
});
expect(2);
var Model = Backbone.Model.extend({
mutators: {
fullname: function () {
return this.firstname + ' ' + this.lastname;
}
}
});
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
});
test("can get 'mutated' value (newly created)", function () {
expect(3);
var Model = Backbone.Model.extend({
mutators: {
fullname: function (attributes) {
return this.firstname + ' ' + this.lastname;
}
}
});
expect(3);
var Model = Backbone.Model.extend({
mutators: {
fullname: function () {
return this.get('firstname') + ' ' + this.get('lastname');
}
}
});
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
equal(model.get('fullname'), 'Sebastian Golasch', 'Can get mutated fullname');
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
equal(model.get('fullname'), 'Sebastian Golasch', 'Can get mutated fullname');
});
test("can get 'mutated' value (overridden)", function () {
expect(5);
var Model = Backbone.Model.extend({
mutators: {
status: function (attributes) {
return { status: this.status, overallStatus: this.overallStatus, underestimatedNonOverallStatus: this.underestimatedNonOverallStatus };
}
}
});
expect(5);
var Model = Backbone.Model.extend({
mutators: {
status: function (key, value, options, set) {
if(key){
set('status', value);
}
var model = new Model();
model.set('overallStatus', 1);
model.set('underestimatedNonOverallStatus', 3);
model.set('status', 2);
return { status: this.attributes.status, overallStatus: this.get('overallStatus'), underestimatedNonOverallStatus: this.get('underestimatedNonOverallStatus') };
}
}
});
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
equal(model.get('underestimatedNonOverallStatus'), 3, 'Can get unmutated underestimatedNonOverallStatus');
equal(model.get('status').status, 2, 'Can get mutated status');
equal(model.get('status').overallStatus, 1, 'Can get mutated status');
equal(model.get('status').underestimatedNonOverallStatus, 3, 'Can get mutated status');
var model = new Model();
model.set('overallStatus', 1);
model.set('underestimatedNonOverallStatus', 3);
model.set('status', 2);
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
equal(model.get('underestimatedNonOverallStatus'), 3, 'Can get unmutated underestimatedNonOverallStatus');
equal(model.get('status').status, 2, 'Can get mutated status');
equal(model.get('status').overallStatus, 1, 'Can get mutated status');
equal(model.get('status').underestimatedNonOverallStatus, 3, 'Can get mutated status');
});
test("can get 'normal' value - object context", function () {
expect(2);
var Model = Backbone.Model.extend({
mutators: {
fullanme: {
get: function (attributes) {
return this.firstname + ' ' + this.lastname;
}
}
}
});
expect(2);
var Model = Backbone.Model.extend({
mutators: {
fullanme: {
get: function () {
return this.get('firstname') + ' ' + this.get('lastname');
}
}
}
});
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
});
test("can get 'mutated' value (newly created) - object context", function () {
expect(3);
var Model = Backbone.Model.extend({
mutators: {
fullname: {
get: function (attributes) {
return this.firstname + ' ' + this.lastname;
}
}
}
});
expect(3);
var Model = Backbone.Model.extend({
mutators: {
fullname: {
get: function () {
return this.get('firstname') + ' ' + this.get('lastname');
}
}
}
});
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
var model = new Model();
model.set('firstname', 'Sebastian');
model.set('lastname', 'Golasch');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
equal(model.get('fullname'), 'Sebastian Golasch', 'Can get mutated fullname');
equal(model.get('firstname'), 'Sebastian', 'Can get unmutated firstname');
equal(model.get('lastname'), 'Golasch', 'Can get unmutated lastname');
equal(model.get('fullname'), 'Sebastian Golasch', 'Can get mutated fullname');
});
test("can get 'mutated' value (overridden) - object context", function () {
expect(5);
var Model = Backbone.Model.extend({
mutators: {
status: {
get: function (attributes) {
return { status: this.status, overallStatus: this.overallStatus, underestimatedNonOverallStatus: this.underestimatedNonOverallStatus };
}
}
}
});
expect(5);
var Model = Backbone.Model.extend({
mutators: {
status: {
get: function () {
return { status: this.attributes.status, overallStatus: this.get('overallStatus'), underestimatedNonOverallStatus: this.get('underestimatedNonOverallStatus') };
}
}
}
});
var model = new Model();
model.set('overallStatus', 1);
model.set('underestimatedNonOverallStatus', 3);
model.set('status', 2);
var model = new Model();
model.set('overallStatus', 1);
model.set('underestimatedNonOverallStatus', 3);
model.set('status', 2);
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
equal(model.get('underestimatedNonOverallStatus'), 3, 'Can get unmutated underestimatedNonOverallStatus');
equal(model.get('status').status, 2, 'Can get mutated status');
equal(model.get('status').overallStatus, 1, 'Can get mutated status');
equal(model.get('status').underestimatedNonOverallStatus, 3, 'Can get mutated status');
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
equal(model.get('underestimatedNonOverallStatus'), 3, 'Can get unmutated underestimatedNonOverallStatus');
equal(model.get('status').status, 2, 'Can get mutated status');
equal(model.get('status').overallStatus, 1, 'Can get mutated status');
equal(model.get('status').underestimatedNonOverallStatus, 3, 'Can get mutated status');
});
test("can set 'normal' value (key <-> value)", function () {
expect(1);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (attributes) {
return { status: this.status, overallStatus: this.overallStatus, underestimatedNonOverallStatus: this.underestimatedNonOverallStatus };
}
}
}
});
expect(1);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function () {
return { status: this.attributes.status, overallStatus: this.get('overallStatus'), underestimatedNonOverallStatus: this.get('underestimatedNonOverallStatus') };
}
}
}
});
var model = new Model();
model.set('overallStatus', 1);
var model = new Model();
model.set('overallStatus', 1);
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
});
test("can set 'mutated' value (key <-> value)", function () {
expect(2);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options) {
this.set('pState', value.pState, options);
this.set('aState', value.aState, options);
}
}
}
});
expect(2);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options) {
this.set('pState', value.pState, options);
this.set('aState', value.aState, options);
}
}
}
});
var model = new Model();
model.set('status', {pState: 1, aState: 2});
var model = new Model();
model.set('status', {pState: 1, aState: 2});
equal(model.get('pState'), 1, 'Can get mutated pState');
equal(model.get('aState'), 2, 'Can get mutated aState');
equal(model.get('pState'), 1, 'Can get mutated pState');
equal(model.get('aState'), 2, 'Can get mutated aState');
});
test("can set 'normal' value (object)", function () {
expect(1);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (attributes) {
return { status: this.status, overallStatus: this.overallStatus, underestimatedNonOverallStatus: this.underestimatedNonOverallStatus };
}
}
}
});
expect(1);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function () {
return { status: this.attributes.status, overallStatus: this.get('overallStatus'), underestimatedNonOverallStatus: this.get('underestimatedNonOverallStatus') };
}
}
}
});
var model = new Model();
model.set({overallStatus: 1});
var model = new Model();
model.set({overallStatus: 1});
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
equal(model.get('overallStatus'), 1, 'Can get unmutated overallStatus');
});
test("can set attribute objects", function () {
expect(3);
var Model = Backbone.Model.extend({
mutators: {
fullname: {
set: function (key, value, options) {
var names = value.split(' ');
this.set('firstname', names[0], options);
this.set('lastname', names[1], options);
}
}
}
});
var model = new Model();
model.set({ fullname: 'Sebastian Golasch', admin: true });
equal(model.get('firstname'), 'Sebastian', 'Can get the firstname');
equal(model.get('lastname'), 'Golasch', 'Can get the lastname');
equal(model.get('admin'), true, 'Can get the admin status');
});
test("can set newly created 'mutated' value (object)", function () {
expect(2);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options) {
this.set('pState', value.pState, options);
this.set('aState', value.aState, options);
}
}
}
});
expect(2);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options) {
this.set('pState', value.pState, options);
this.set('aState', value.aState, options);
}
}
}
});
var model = new Model();
model.set({status: {pState: 1, aState: 2, dState: 3}});
var model = new Model();
model.set({status: {pState: 1, aState: 2, dState: 3}});
equal(model.get('pState'), 1, 'Can get mutated pState');
equal(model.get('aState'), 2, 'Can get mutated aState');
equal(model.get('pState'), 1, 'Can get mutated pState');
equal(model.get('aState'), 2, 'Can get mutated aState');
});
test("can set 'mutated' value (object)", function () {
expect(4);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
this.set('pState', value.pState, options);
this.set('aState', value.aState, options);
if (value.pState === 1) {
set(key, 'supercool', options);
}
}
}
},
defaults: {
status: 'awkward'
}
});
expect(4);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
if(_.isString(value)){
set(key, value);
} else {
this.set('pState', value.pState, options);
this.set('aState', value.aState, options);
if (value.pState === 1) {
set(key, 'supercool', options);
}
}
}
}
},
defaults: {
status: 'awkward'
}
});
var model = new Model();
var model = new Model();
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set({status: {pState: 1, aState: 2, dState: 3}});
equal(model.get('status'), 'supercool', 'Can get mutated status value');
equal(model.get('pState'), 1, 'Can get mutated pState');
equal(model.get('aState'), 2, 'Can get mutated aState');
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set({status: {pState: 1, aState: 2, dState: 3}});
equal(model.get('status'), 'supercool', 'Can get mutated status value');
equal(model.get('pState'), 1, 'Can get mutated pState');
equal(model.get('aState'), 2, 'Can get mutated aState');

@@ -232,25 +262,25 @@ });

test("can set 'mutated' value and fire event", function () {
expect(3);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
set(key, value.toLowerCase(), options);
}
}
},
defaults: {
status: 'awkward'
}
});
expect(3);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
set(key, value.toLowerCase(), options);
}
}
},
defaults: {
status: 'awkward'
}
});
var model = new Model();
var model = new Model();
model.bind('mutators:set:status', function () {
ok(true, 'Callback called');
});
model.bind('mutators:set:status', function () {
ok(true, 'Callback called');
});
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set({status: 'SUPERCOOL'});
equal(model.get('status'), 'supercool', 'Can get mutated status value');
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set({status: 'SUPERCOOL'});
equal(model.get('status'), 'supercool', 'Can get mutated status value');

@@ -260,25 +290,25 @@ });

test("can set 'mutated' value and fire event", function () {
expect(2);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
set(key, value.toLowerCase(), options);
}
}
},
defaults: {
status: 'awkward'
}
});
expect(2);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
set(key, value.toLowerCase(), options);
}
}
},
defaults: {
status: 'awkward'
}
});
var model = new Model();
var model = new Model();
model.bind('mutators:set:status', function () {
ok(true, 'Callback called (And this shouldn´t happen)');
});
model.bind('mutators:set:status', function () {
ok(true, 'Callback called (And this shouldn´t happen)');
});
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set('status', 'SUPERCOOL', {silent: true});
equal(model.get('status'), 'supercool', 'Can get mutated status value');
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set('status', 'SUPERCOOL', {silent: true});
equal(model.get('status'), 'supercool', 'Can get mutated status value');

@@ -288,29 +318,29 @@ });

test("can set 'mutated' value and fire event", function () {
expect(3);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
set(key, value.toLowerCase(), options);
}
}
},
defaults: {
status: 'awkward'
}
});
expect(3);
var Model = Backbone.Model.extend({
mutators: {
status: {
set: function (key, value, options, set) {
set(key, value.toLowerCase(), options);
}
}
},
defaults: {
status: 'awkward'
}
});
var model = new Model();
var model = new Model();
model.bind('mutators:set:status', function () {
ok(true, 'Callback called (And this shouldn´t happen)');
});
model.bind('mutators:set:status', function () {
ok(true, 'Callback called (And this shouldn´t happen)');
});
model.bind('change:status', function () {
ok(true, 'Callback called (And this should happen)');
});
model.bind('change:status', function () {
ok(true, 'Callback called (And this should happen)');
});
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set('status', 'SUPERCOOL', {mutators: {silent: true}});
equal(model.get('status'), 'supercool', 'Can get mutated status value');
equal(model.get('status'), 'awkward', 'Can get unmodified value');
model.set('status', 'SUPERCOOL', {mutators: {silent: true}});
equal(model.get('status'), 'supercool', 'Can get mutated status value');

@@ -320,31 +350,90 @@ });

test("can serialize an unmutated model", function () {
expect(2);
var Model = Backbone.Model.extend({
defaults: {
a: 'a',
b: 'b'
}
});
expect(2);
var Model = Backbone.Model.extend({
defaults: {
a: 'a',
b: 'b'
}
});
equal((new Model()).toJSON().a, 'a', 'can serialize unmutated model');
equal((new Model()).toJSON().b, 'b', 'can serialize unmutated model');
equal((new Model()).toJSON().a, 'a', 'can serialize unmutated model');
equal((new Model()).toJSON().b, 'b', 'can serialize unmutated model');
});
test("can serialize an unmutated model", function () {
expect(3);
var Model = Backbone.Model.extend({
defaults: {
a: 'a',
b: 'b'
},
mutators: {
state: function () {
return this.a + ', ' + this.b;
}
}
});
test("can serialize mutated model", function () {
expect(3);
var Model = Backbone.Model.extend({
defaults: {
a: 'a',
b: 'b'
},
mutators: {
state: function () {
return this.get('a') + ', ' + this.get('b');
}
}
});
equal((new Model()).toJSON().a, 'a', 'can serialize mutated model');
equal((new Model()).toJSON().b, 'b', 'can serialize mutated model');
equal((new Model()).toJSON().state, 'a, b', 'can serialize mutated model');
});
equal((new Model()).toJSON().a, 'a', 'can serialize mutated model');
equal((new Model()).get('state'), 'a, b', 'can serialize mutated model');
equal((new Model()).toJSON().state, 'a, b', 'can serialize mutated model');
});
test("can escape mutated properties", function () {
expect(2);
var Model = Backbone.Model.extend({
defaults: {
a: 'a',
b: 'b'
},
mutators: {
b: function () {
return 'c';
}
}
});
var model = new Model();
equal(model.get('b'), 'c');
model.set('b', 'foobar');
equal(model.get('b'), 'c');
});
test("can get/set using single method", 6, function(){
var Model = Backbone.Model.extend({
mutators:{
state:function(key, value){
if(key){
this.set("a", value);
equal(arguments.length, 4);
return null; //prevents ret from returning
}
return this.get("a");
}
}
});
var model = new Model();
var value = "happy";
model.set('state', value);
equal(model.get('state'), value);
var new_state = "excited",
new_level = 10;
//set multiple
model.set({
level:new_level,
state:new_state
});
equal(model.get('state'), new_state);
equal(model.get('level'), new_level);
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc