// FLUX using Backbone evnts
// ActoinName+data >> Dispatcher >> Store(applys changes) > emit change >> views(pull new state data)
//=====================================================
//========================================= Data Modles
//=====================================================
var ToDo = Backbone.Model.extend({
initialize: function(){
this.set({
id:(new Date()).getTime(),
created: new Date()
})
}
});
var ToDos = Backbone.Collection.extend({
model: ToDo
});
var DATA = new ToDos()
//=====================================================
//========================================== Dispatcher
//Take the brocasted Action and find what Store to tell
var Dispatcher = Backbone.Events
//=====================================================
//=============================================== Store
//============================================ App data
var ACTIONS = {
TODO:{
ADD:"ADD_TODO",
DEL:"DEL_TODO"
}
}
var Store = function(DATA,rules){
// ++ Stores should recive all actions ++
this.auto = Dispatcher.on("all",rules),
this.mixins = {
componentWillMount: function() {
// ++ emit change if underlying data changes ++
DATA.on("all", (function(){this.forceUpdate()}).bind(this));
},
componentWillUnmount: function() {
DATA.off("all", (function(){this.forceUpdate()}).bind(this));
}
},
this.getData = function(){
return DATA.toJSON();
}
}
var toDoStore = new Store(DATA, function(actionName,data){
switch(actionName){
case ACTIONS.TODO.ADD:
DATA.add(new ToDo({text:data.text}))
break;
case ACTIONS.TODO.DEL:
DATA.remove(data.id)
break;
}
});
//=====================================================
//================================================ View
//=====================================================
/**...
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.