Skip to content

Instantly share code, notes, and snippets.

@walterg2
Created June 13, 2012 11:51
Show Gist options
  • Save walterg2/2923614 to your computer and use it in GitHub Desktop.
Save walterg2/2923614 to your computer and use it in GitHub Desktop.
Issue when attempting to initialize a Backbone model with invalid data

A quick note regarding my set up:

  • I started this so I could really dig into Backbone Models and (maybe) Collections
  • Using Jasmine 1.2.0 for my testing framework
  • I had changed the test for "should not allow for me to create a character without a name" to just check the isValid() attribute to get by the issue there, but when I ran into it again with Alignment, it bothered me enough to attempt to fix
  • Yes, this IS the Evercraft Kata in Backbone... At least, where I've made it to before running into the problem.
/*jslint nomen: true, regexp: true */
/*globals window, document, Backbone, _ */
var Character = Backbone.Model.extend({
alignments: {
"Evil": "Evil",
"Neutral": "Neutral",
"Good": "Good"
},
validate: function (attrs) {
"use strict";
if (!(/\S/.test(attrs.name))) {
return "You must set a name for this character";
}
if (!!(/\S/.test(attrs.alignment)) || !this.alignments.hasOwnProperty(attrs.alignment)) {
return "Invalid alignment given";
}
},
getName: function () {
"use strict";
return this.get("name");
},
getAlignment: function () {
"use strict";
return this.get("alignment");
},
initialize: function () {
"use strict";
this.bind("error", function (model, error) {
window.console.log("Error: " + error);
model = null;
});
var error = this.validate(this.attributes);
if (error) {
this.trigger('error', this, error);
}
}
});
/*globals describe, beforeEach, afterEach, it, expect, Character */
describe("EverCraft", function () {
"use strict";
describe("Characters", function () {
it("should be defined", function () {
expect(Character).toBeDefined();
});
describe("Attributes", function () {
var character;
beforeEach(function () {
character = new Character({"name": "Balrock", "alignment": "Good"});
});
afterEach(function () {
});
describe("#Name", function () {
it("should be defined", function () {
expect(character.getName).toBeDefined();
});
it("should allow for me to set the character's name to 'Balrock'", function () {
expect(character.getName()).not.toBeNull();
});
it("should not allow for me to create a character without a name", function () {
character = new Character();
expect(character).toBeNull();
});
it("should allow for me to retrieve the character's name", function () {
expect(character.getName()).toEqual("Balrock");
});
});
describe("#Alignment", function () {
it("should be defined", function () {
expect(character.getAlignment).toBeDefined();
});
it("should allow for me to set the character's alignment to 'Good'", function () {
expect(character.getAlignment()).toEqual("Good");
});
it("should allow for me to set the character's alignment to 'Evil'", function () {
character = new Character({"name": "Balrock", "alignment": "Evil"});
expect(character.getAlignment()).toEqual("Evil");
});
it("should allow for me to set the character's alignment to 'Neutral'", function () {
character = new Character({"name": "Balrock", "alignment": "Neutral"});
expect(character.getAlignment()).toEqual("Neutral");
});
it("should not allow for me to create a character with an alignment set to 'Nonsense'", function () {
character = new Character({"name": "Balrock", "alignment": "Nonsense"});
expect(character).toBeNull();
});
});
});
});
});
@davemo
Copy link

davemo commented Jun 13, 2012

Note, you could write your own version of a Backbone.Model using Backbone.Model.extend and override the initialize function such that it does what you want. But I think it's easier to bend your way of thinking to the workflow Backbone anticipates and code accordingly :)

@walterg2
Copy link
Author

Yeah, given that I'm doing this to get better at Backbone, I don't think I want to change it (just yet).

Just a downside to working this through a kata and not an actual application. (although, maybe a small Sinatra server for the workshop is in order to do saves and such... Ah, the possibilities)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment