//LOOKING AT MODELS A BIT CLOSER
// Enter the example code here
var Person = new Backbone.Model();
console.log(Person.name);
Person.on("change:name", function() {console.log("the name has been changed")});
Person.set({name: "Mehmetcan"});
console.log("attributes are", Person.attributes.name);
console.log(Person.hasChanged("name"));
//it is good to listen for changes in the initialize function, which is called when the model is initialized.
//model change listeners
var Todo = Backbone.Model.extend({
defaults: {
title: '',
completed: false
},
initialize: function(){
console.log("a new model has been initialized");
this.on("change:title", function(){
console.log("the title has been changed");
});
},
setTitle: function(newTitle) {
this.set({title: newTitle});
},
});
var todo = new Todo();
//if you set values through the attributes attribute, you can bypass the event listeners
todo.set('attritutes.title', "joe");
//if you don't use the attributes, it will trigger the listener
todo.set({title: "mehmetcan"});
//model instances attributes are gotten by the get method
console.log(todo.get('title'));
//DEFINE A TODO MODEL AND CREATE A TODO INSTANCE
//you extend the backbone model to have the following attributes and build a Todo constructor
var Todo = Backbone.Model.extend({
defaults: {
title: '',
completed: false
}
});
//in backbone you can change the attribute of a model instance while creating it.
var myTodo = new Todo({title: 'Check attributes property of the logged module in the console'});
//DEFINE VIEWS WHICH HAS HANDLER AND CONTROLLING LOGIC
var TodoView = Backbone.View.extend({
tagname: 'li', //this creates a 'li' element which is referenced by the 'el' property
todoTpl: Handlebars.compile($('#item-template').html()),
//this is the event handling logic. The Backbone events hash allows us to attach event...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.