ftlabs / fruitmachine

View rendering engine
MIT License
247 stars 18 forks source link

Could helpers list dependencies on other helpers [recursively]? #33

Closed matthew-andrews closed 11 years ago

matthew-andrews commented 11 years ago

Say I wanted to create a helper then wanted to be able to use this.delegate inside that 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.

wilsonpage commented 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 .

matthew-andrews commented 11 years ago

Will do

matthew-andrews commented 11 years ago

I now realise that what I was trying to implement was probably an anti-pattern.

  1. It was magic. And not in a good way.
  2. It wasn't making use of any of the events logic of fruitmachine even though it was dealing with events.
  3. data-action - essentially putting non-native html controllery logic inside html source = crap.
  4. Too opinionated to be forcing superagent down people's throats, even if it is glorious.

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.