Skip to content

Instantly share code, notes, and snippets.

@bdadam
Last active February 28, 2017 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdadam/b21582d3c9a4c039326cdc46819f2d44 to your computer and use it in GitHub Desktop.
Save bdadam/b21582d3c9a4c039326cdc46819f2d44 to your computer and use it in GitHub Desktop.
Composition
const vehicleToHtml = obj => {
return `<div id="${obj.id}">${obj.name}</div>`;
};
$.ajax({
url: '/get/all/vehicles',
success: vehicles => {
const html = vehicles.map(vehicleToHtml);
// const html = vehicles.map(v => new Vehicle(v)).map(v => v.toHtml());
document.querySelector('#list').innerHTML = html;
}
});
// instead of:
class Vehicle {
constructor(data) {
this.id = data.id;
this.name = data.name;
}
toHtml() {
return `<div id="${this.id}">${this.name}</div>`;
}
}
const db = {
find(query) {},
findById(id) {},
create(obj) {},
delete(id) {}
};
export default db;
const createUser = db => ({
create(userData) { db.create(userData); },
delete(userID) {db.delete(userID); }
});
export default createUser;
import db from './3-db';
import userModel from './4-userModel';
const User = userModel(db);
User.create({ name: 'Mr. Qwe Asd', age: 100, city: 'Munich' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment