Skip to content

Instantly share code, notes, and snippets.

@kazu69
Created May 26, 2012 06:54
Show Gist options
  • Save kazu69/2792648 to your computer and use it in GitHub Desktop.
Save kazu69/2792648 to your computer and use it in GitHub Desktop.
Backbone.jsでのmodelのsaveイベントをsinonjsでテスト
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Backbone + sinon simple tests</title>
<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src='http://documentcloud.github.com/underscore/underscore-min.js'></script>
<script type="text/javascript" src='http://documentcloud.github.com/backbone/backbone.js'></script>
<script type="text/javascript" src='http://sinonjs.org/releases/sinon-1.3.4.js'></script>
<script src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js" type="text/javascript"></script>
<script type="text/javascript">
// BackBone Model
var model;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
model = (function() {
__extends(model, Backbone.Model);
function model() {
model.__super__.constructor.apply(this, arguments);
}
model.prototype.url = 'http://jsfiddle.net/echo/json/';
model.prototype.validate = function(attrs) {
var erros;
erros = {};
errors.msg = 'always error return';
if (!_.isEmpty(errors)) {
return errors;
}
};
return model;
})();
// Backbone View
var view;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
view = (function() {
__extends(view, Backbone.View);
function view() {
view.__super__.constructor.apply(this, arguments);
}
view.prototype.initialize = function() {
return this.model.bind('error', (function(model, error) {
if (null !== error || 'undefined' !== error) {
return this.show(error);
}
}), this);
};
view.prototype.show = function() {
return console.log('fired show');
};
return view;
})();
this.Model = new model();
this.View = new view({model:this.Model});
var spy = sinon.spy(View, 'show')
this.View.model.save();
//saveイベント終了後に評価する
setTimeout(function(){console.log(spy.called)}, 1000);
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment