Created
April 1, 2015 08:15
-
-
Save ktkaushik/c5ae6fc337aba7dad436 to your computer and use it in GitHub Desktop.
A simple Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
var model = new BackboneModel({ | |
key : value | |
}) | |
model.add("key", "value") | |
model.listen("key", function(){ | |
}) | |
*/ | |
/** | |
* Spine Model | |
* | |
* This is a simple SpineModel which can be used to set key-value pairs | |
* and add listeners to have complex functionalities in the future. | |
* | |
* add - (key, value) adds key and value to the model instance | |
* set - (obj) adds the obj's keys and values to the existing model instance | |
* on - (eventName, cb) adds events | |
* emit - (eventName) triggers an event | |
* onchange - (key) listens to changes on a particular key in the model instance | |
* | |
* Author - Kaushik Thirthappa (thirthappa.kaushik@gmail.com) | |
*/ | |
function SpineModel () { | |
var events = {}; | |
// custom setter | |
this.add = function (key, value) { | |
this[key] = value; | |
} | |
// custom setter | |
this.set = function (obj) { | |
for(var key in obj) { | |
this[key] = obj[key]; | |
} | |
} | |
// on event | |
this.on = function (eventName, cb) { | |
if (!events.hasOwnProperty(eventName)) { | |
events[eventName] = cb; | |
} | |
} | |
// emit those events | |
this.emit = function (eventName) { | |
if (events.hasOwnProperty(eventName)) { | |
console.log('the event %s is being triggered !!', eventName); | |
events[eventName](); | |
} else { | |
return; | |
} | |
} | |
// adding listeners | |
this.onchange = function (key) { | |
console.log('setting onchange to %s', key); | |
Object.defineProperty(this, key, { | |
get: function() { | |
return key; | |
}, | |
set: function(value) { | |
console.log('setting the variable'); | |
key = value; | |
} | |
}); | |
} | |
} | |
// Execute | |
var model = new SpineModel(); | |
/** | |
* Apologies on the loggers | |
*/ | |
console.log('******************** Adding a as a new key with 10 as value using add function ********************'); | |
model.add('a', 10); | |
console.log(model.a); | |
console.log('******************** Loggin model right after ********************'); | |
console.log('******************** Adding name as a new key with Dron as value using set function ********************'); | |
model.set({name: 'Dron'}); | |
console.log(model.name); | |
console.log('******************** Loggin model right after ********************'); | |
console.log('******************** Adding an event now ********************'); | |
model.on('logger', function() { | |
console.log('finished executing the event.......'); | |
}); | |
model.emit('logger'); | |
console.log('******************** Triggering an event now ********************'); | |
model.onchange('name'); // add listener | |
model.name = 'Kaushik'; | |
console.log(' ----- changing the name after adding listener using native assignment operator -----'); | |
console.log(model.name); | |
console.log('******************** Logging the model name ********************') | |
console.log(' ----- changing the name after adding listener using set for spineModel ------'); | |
model.set({name: 'shruti'}); | |
console.log(model.name); | |
console.log('******************** Logging the model name ********************') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment