Skip to content

Instantly share code, notes, and snippets.

var Person = Backbone.Model.extend({
promptName: function() {
var name = prompt("Please enter your name:");
this.set({name: name});
}
});
window.person = new Person;
var myCallback = function(personModel, enteredName) {
alert(‘Hello’ + enteredName);
};
person.bind('change:name', myCallback);
person.promptName();
window.person = new Person;
var myCallback = function(personModel, enteredName) {
alert(‘Hello’ + enteredName);
};
person.bind('change:name', myCallback);
person.promptName();
var PersonModel = Backbone.Model.extend();
var PersonView = Backbone.View.extend({
initialize: function () {
_.bind(this, 'render’);
this.model.bind(‘change:name’, this.render);
},
render: function () {
$(‘body’).html(this.model.get(‘name’));
}
});
var persons = new Backbone.Collection();
persons.bind("add", function(person) {
alert("Servus " + person.get("name") + "!");
});
persons.add([
{name: "Marcus"},
{name: "Maria"},
]);
var myRouter = Backbone.Router.extend({
routes: {
"gallery/:image": "gallery"
},
gallery: function(image) {
// do something nice
}
});
<html>
<head>
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery.tmpl.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
<script src="backbone.js"></script>
</head>
<body>
You have <span class="js-wish-counter">0</span> wishes.
<!-- VIEW -->
My name is <b data-bind=”text: personName“></span>
// VIEW-MODEL
var viewModel = {
personName: ’Marco Francke’
};
// ACTIVATE KNOCKOUT
ko.applyBindings(viewModel);
My name is <b>Marco Francke</b>
var viewModel = {
personName: ko.obervable(’Marco Francke’)
};