Closed gr2m closed 10 years ago
Okay, I found the problem, wrong ampersand-view
version :) Which is odd, because I run npm install --save ampersand-view
before.
But anyway, I'd like to strongly support the suggestions for the shorthand subview declarations at https://github.com/AmpersandJS/ampersand-view/issues/43, feels more than necessary repetitive code right now. For reference, here's the code I ended up with:
var View = require('ampersand-view');
var AmpersandModel = require('ampersand-model');
var AmpersandCollection = require('ampersand-collection');
var domready = require('domready');
// VIEWS
var ConsoleView = View.extend({
template: '\
<div data-hook="console">\
> <span data-hook="command"></span>\
</div>',
bindings: {
'model.name': {
type: 'text',
hook: 'command'
}
}
});
var LogView = View.extend({
template: '\
<div data-hook="log">\
<h2>Log</h2>\
<ul data-hook="commands"></ul>\
</div>',
render: function() {
this.renderWithTemplate(this);
this.renderCollection(this.collection, LogItemView, this.queryByHook('commands'));
return this;
}
});
var LogItemView = View.extend({
template: '<li data-hook="command"></li>',
bindings: {
'model.name': {
type: 'text',
hook: 'command'
}
}
})
var MainView = View.extend({
template: '\
<body>\
<h1>Hoodie Console</h1>\
<div data-hook="console">\
</div>\
<div data-hook="log">\
</div>\
</body>',
autoRender: true,
subviews: {
input: {
hook: 'console',
waitFor: 'model',
constructor: ConsoleView,
prepareView: function (el) {
return new this.subviews.input.constructor({
el: el,
parent: this,
model: this.model
});
}
},
log: {
hook: 'log',
waitFor: 'collection',
constructor: LogView,
prepareView: function (el) {
return new this.subviews.log.constructor({
el: el,
parent: this,
collection: this.collection
});
}
}
},
});
// MODELS
var Command = AmpersandModel.extend({
props: {
name: ['string', true, '']
}
});
var CommandCollection = AmpersandCollection.extend({
model: Command
});
var app = window.app = {};
domready(function () {
app.command = new Command({name: 'hoodie.'});
app.commands = new CommandCollection([{
name: 'hoodie.account.signUp("foo", "bar")'
},{
name: 'hoodie.store.findAll()'
},{
name: 'hoodie.id()'
}]);
self.view = new MainView({
el: document.body,
model: app.command,
collection: app.commands
});
});
Roger that! :+1:
@gr2m finally sat down to look at this (i had seen your tweet before) but looks like you figured it out.
I try to build a simple app showing a console and a log of the executed commands. I have a main view and two subviews: one for the current command, and one for the log:
The resulting HTML currently looks like
I'd have expected
I'm sure I get something very basic wrong, I'm just getting started with &.js. Could you please help me to get this right?
Here's my package.json for reference