Skip to content

Instantly share code, notes, and snippets.

@burtlo
Created July 16, 2013 18:00
Show Gist options
  • Save burtlo/6011059 to your computer and use it in GitHub Desktop.
Save burtlo/6011059 to your computer and use it in GitHub Desktop.
Bob = function() {
this.hey = function(message) {
if (this.isSilence(message)) {
return "Fine, be that way.";
} else if (this.isShouting(message)) {
return "Woah, chill out!";
} else if (this.isAQuestion(message)) {
return "Sure";
} else {
return 'Whatever';
}
};
this.isSilence = function(message) {
return message === "";
}
this.isShouting = function(message) {
return message.toUpperCase() === message;
}
this.isAQuestion = function(message) {
return message[message.length -1] === "?";
}
};
$(function() {
var Saying = Backbone.Model.extend({});
var SayingsCollection = Backbone.Collection.extend({
model: Saying,
localStorage: new Backbone.LocalStorage("sayings-backbone")
});
Sayings = new SayingsCollection;
var FavoritesView = Backbone.View.extend({
el: $("#bobFavoriteResponses"),
initialize: function() {
console.log("initializing bob favorite responses view");
this.listenTo(Sayings, 'add', this.addOne);
},
addOne: function(model) {
console.log("Adding a new favorite saying");
var html = $("#bobResponse").html();
var template = Handlebars.compile(html);
var templateResult = template({response: model.get("response")});
var destination = $("#bobFavoriteResponses");
destination.append(templateResult);
}
});
var favoritesView = new FavoritesView;
var AppView = Backbone.View.extend({
el: $("#bobForm"),
events: {
"submit" : "askBob"
},
initialize: function() {
console.log("initializing bob app view");
this.listenTo(Sayings, 'add', this.addOne);
},
askBob: function(e) {
e.preventDefault();
console.log("Asking Bob Something");
var statement = $("#askBobText").val();
var bob = new Bob();
var response = bob.hey(statement);
Sayings.create({ response: response });
},
addOne: function(model) {
console.log("Adding this model");
var html = $("#bobFlashyResponse").html();
var template = Handlebars.compile(html);
var templateResult = template({response: model.get("response")});
var destination = $("#bobResponses");
destination.append(templateResult);
}
});
var App = new AppView;
});
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Bob</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="underscore-1.5.1.js"></script>
<script src="backbone-1.0.0.js"></script>
<script src="backbone-localStorage.js"></script>
<script src="handlebars-1.0.0.js"></script>
<script src="bob.js"></script>
</head>
<body>
<div id='bobFavoriteResponses'>
<h2>Bobs Favorite Responses</h2>
<ul>
</ul>
</div>
<h1>Bob</h1>
<form id="bobForm">
<fieldset>
<legend>Ask Bob Something</legend>
<input type='text' name='askBobInput' id='askBobText' autofocus="autofocus" />
<input type='submit' id='askBob' value='Ask' />
</fieldset>
</form>
<div id='bobResponses'>
<h2>Bobs Responses</h2>
<ul>
</ul>
</div>
<script id="bobResponse" type="text/x-handlebars-template">
<li>{{response}}</li>
</script>
<script id="bobFlashyResponse" type="text/x-handlebars-template">
<li style="color: blue;">{{response}}</li>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment