Created
November 23, 2016 14:52
-
-
Save 3mcd/07ab935e1c4a1d4eee904c77360e016a to your computer and use it in GitHub Desktop.
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
const _ensureNew = (ctor) => { | |
const fn = (...args) => !(this instanceof ctor) ? new ctor(...args) : ctor(...args); | |
fn.prototype = ctor.prototype; | |
return fn; | |
} | |
const ensureNew = (ctor) => { | |
const fn = _ensureNew(ctor); | |
fn.extend = Backbone.Model.extend; | |
return fn; | |
} | |
const BaseView = ensureNew(Backbone.View.extend({ | |
r(strs, ...subs) { | |
return strs.reduce((a, x, i) => { | |
const sub = subs[i - 1]; | |
return a + (sub instanceof BaseView ? sub.render() : sub) + x; | |
}); | |
} | |
})); | |
const Nav = BaseView.extend({ | |
links: [ { url: 'http://www.google.com', text: 'Google' } ], | |
render() { | |
return this.r` | |
${this.links.map(({url, text}) => `<a href="${url}>${text}</a>"`)} | |
`; | |
} | |
}); | |
const App = BaseView.extend({ | |
render() { | |
return this.r` | |
<header> | |
${Nav()} | |
</header> | |
`; | |
} | |
}); | |
const app = App(); | |
app.render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment