Created
November 13, 2012 18:29
-
-
Save k33g/4067501 to your computer and use it in GitHub Desktop.
Bob
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
/* --- quick starter --- */ | |
/// <reference path="../../ts/lib/bob.ts"/> | |
declare var $: any; | |
declare var Mustache: any; | |
//define human model | |
class Human extends Bob.Model {} | |
//define collection of human | |
class FringeDivision extends Bob.Collection{ | |
model = Human; | |
update (event) { //triggered when model change (collection is an observer) | |
this.notifyObservers(event); //collection notifies her observers (collection is observable) | |
} | |
} | |
//define view | |
class FringeView extends Bob.View { | |
engine = Mustache.to_html; | |
template = $("#list_template").html(); | |
target = null; | |
constructor (selector:string){ | |
super(); | |
this.target = $(selector); | |
} | |
} | |
//define controller | |
class FringeController extends Bob.Controller { | |
view = new FringeView("#list"); | |
constructor(public collection:Bob.Collection){ | |
super(); | |
collection.registerObserver(this); | |
} | |
displayHumans() {//call render() method of the view | |
this.view.render({ | |
humans : this.collection.toJSON() | |
}); | |
//listen to events view | |
this.view.events([ | |
{ | |
selector:"#list ul >li", | |
event:"click", | |
action : (event)=> { | |
console.log(event.target.dataset); | |
} | |
} | |
]); | |
} | |
update (event) { //triggered when "observables" change (controller is an observer) | |
//update display when collection change | |
this.view.render({ | |
humans : this.collection.toJSON() | |
}); | |
} | |
} | |
//Go, run it | |
var Olivia = new Human({firstName:"Olivia", lastName:"Dunham"}); | |
var Astrid = new Human({firstName:"Astrid", lastName:"Farnsworth"}); | |
var Peter = new Human({firstName:"Peter", lastName:"Bishop"}); | |
var Walter = new Human({firstName:"Walter", lastName:"Bishop"}); | |
var fringeDivision = new FringeDivision([Olivia, Astrid, Peter, Walter]); | |
var fringeController = new FringeController(fringeDivision); | |
//Display data | |
fringeController.displayHumans(); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<title>bob</title> | |
</head> | |
<body> | |
<script type="text/template" id="list_template"> | |
<ul> | |
{{#humans}} | |
<li> | |
{{firstName}} {{lastName}} | |
</li> | |
{{/humans}} | |
</ul> | |
</script> | |
<div id="list"></div> | |
</body> | |
<script src="../lib/vendors/jquery-1.8.2.js"></script> | |
<script src="../lib/vendors/mustache.js"></script> | |
<script src="../../ts/lib/bob.js"></script> | |
<script src="quick.js"></script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment