Closed matthew-andrews closed 11 years ago
Look into how helpers/breakpoint depends on helpers/resize to see how I solved this issue.
On Sun, May 19, 2013 at 12:54 PM, Matt Andrews notifications@github.comwrote:
Say I wanted to create a helper than wanted to be able to use this.delegate inside a view helper - so I list delegate as a dependency of my helper - (say I also use it in my view's FruitMachine definition file) - and then FruitMachine would ensure that it would only add delegate once and it would add it before any of the other helpers that depend on it.
— Reply to this email directly or view it on GitHubhttps://github.com/wilsonpage/fruitmachine/issues/33 .
Will do
I now realise that what I was trying to implement was probably an anti-pattern.
So I decided that pulling out the ajax part and asking the dev to do that plus the ajax callback would be best. However doing so and you're left with:
module.exports = function(view) {
view.on('initialize', function() {
this._onFormSubmit = this._onFormSubmit.bind(this);
});
view.on('setup', function() {
var formEl = require('dom').elementByTag('form', this.el); /* 1 */
formEl.addEventListener('submit', this._onFormSubmit);
this._formEl = formEl;
});
view.on('teardown', function() {
this._formEl.removeEventListener('submit', this._onFormSubmit);
});
};
* 1 - dom
Considering I was wanting to depend on the dom-delegate library anyway I realise now the best solution to the problem I was trying to solve was: just use fruitmachine-domdelegate on its own.
var Apple = fruitmachine.define({
name: 'apple',
helpers: [require('fruitmachine-domdelegate')],
initialize: function() {
this.onFormSubmit = this.onFormSubmit.bind(this);
},
setup: function() {
this.delegate.on('submit', 'form', this.onFormSubmit);
},
onFormSubmit: function() {
this.fire('formsubmit');
}
});
Simples.
Separately I'm going to try to get test coverage done for fruitmachine-domdelegate asap so we can put it on github.com/ftlabs and so we can add a new "helpers" section to the fruitmachine README.md doc.
Say I wanted to create a helper then wanted to be able to use
this.delegate
inside that view helper - so I listdelegate
as a dependency of my helper - (say I also use it in my view's FruitMachine definition file) - and then FruitMachine would ensure that it would only add delegate once and it would add it before any of the other helpers that depend on it.