Last active
August 29, 2015 13:56
-
-
Save chrisbuttery/8795430 to your computer and use it in GitHub Desktop.
Using reactive to populate a list of user data
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
<form> | |
<div class="js-formItem"> | |
<input type="text" value="{fullName}"></input> | |
<input type="text" value="{HomePhone || '555 555 555'}"></input> | |
</div> | |
</form> |
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
module.exports = function(){ | |
var reactive = require('reactive'); | |
var Emitter = require('emitter'); | |
var obj = { | |
"Customers": [ | |
{ | |
"FirstName": "Ali", | |
"Surname": "Rad", | |
"HomePhone": "02 4991 2111" | |
}, | |
{ | |
"FirstName": "Chadia", | |
"Surname": "Rad", | |
"HomePhone": "02 4991 2111" | |
}, | |
{ | |
"FirstName": "Farah", | |
"Surname": "Rad", | |
"HomePhone": "02 4991 2111" | |
} | |
] | |
}; | |
// get the template and hide it | |
var tmpl = document.querySelector('.js-formItem'); | |
tmpl.parentNode.removeChild(tmpl); | |
// the Constructor | |
function Customer(model) { | |
this.model = model; | |
} | |
// the Constructor view | |
Customer.prototype.view = function(){ | |
var self = this; | |
var el = tmpl.cloneNode(true); | |
reactive(el, this.model, { | |
fullName: function(){ | |
self.emit('joined name', self.model); | |
return self.model.FirstName + ' ' + self.model.Surname; | |
} | |
}); | |
this.emit('added ', self.model.FirstName); | |
return el; | |
}; | |
Emitter(Customer.prototype); | |
// Iterate the customers, create views and append | |
obj.Customers.forEach(function(cust) { | |
var customer = new Customer(cust); | |
customer.on('joined name', function(data){ | |
console.log('data ', data); | |
}); | |
document.body.appendChild(customer.view()); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment