Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component library #81

Closed
explorigin opened this issue May 9, 2014 · 3 comments
Closed

Component library #81

explorigin opened this issue May 9, 2014 · 3 comments

Comments

@explorigin
Copy link

OT: Congratulations on the speed http://jsperf.com/angular-vs-knockout-vs-ember/308

Just dipping my toe into mithril reveals it's power and yet a pain-point. Any tool with lots of power has the potential to modularize. It would be cool if there was a place on the website or somewhere else that would publish components. (In the Knockout world, there is a section in the wiki for recipes.)

I'll start with a simple select component:

// Example usage:
//
// In your controller:
// this.select = new Select.controller({
//     caption: 'Please select an option',
//     options: {
//         'key1': 'Label 1',
//         'key2': 'Label 2'
//     }
// });
//
// Include in your view like:
// new Select.view(ctrl.select)
//

var Select;

Select = {
  controller: function(data) {
    this.classes = data.classes || '';
    this.value = m.prop(data.value);
    this.caption = data.caption;
    this.options = data.options || {};
    return this;
  },
  view: function(ctrl) {
    var option = function(key) {
      return m('option', {value: key}, ctrl.options[key]);
    };
    return [
      m("select", {
        onchange: m.withAttr("value", ctrl.value),
        value: ctrl.value(),
        'class': ctrl.classes
      }, [
        ctrl.caption ? m('option', {
          value: ''
        }, ctrl.caption) : '', Object.keys(ctrl.options).map(option)
      ])
    ];
  }
};
@lhorie
Copy link
Member

lhorie commented May 9, 2014

Yeah, definitely need a place to start aggregating community-authored components. There's already a few gems floating around in various places.

Generally, I think it would be good to have github repos for these things, so they can have a proper home (with issue tracking, forkability, etc). We could start using the wiki feature in Github to link to the various projects, and I could add a link to the wiki into the main site as well.

@lhorie
Copy link
Member

lhorie commented May 10, 2014

I added a link to the wiki in the community page. It'll show up on the site when 0.1.13 is released

@lhorie lhorie closed this as completed May 10, 2014
@jsguy
Copy link
Contributor

jsguy commented Aug 22, 2014

Hi guys,

Just a quick comment - if we use a bi-directionally bound model, this kind of component is not really necessary. I have such bindings here:

https://github.com/jsguy/mithril.bindings

Using it, we get this in the model:

this.selectValue = m.p();
this.options = m.p([
    { value: '', text: 'Select an option' },
    { value: 'key1', text: 'Label 1' },
    { value: 'key2', text: 'Label 2' }
]);

In the view:

m("select", { value: data.selectValue }, [
    data.options().map(function(opt) {
        return m('option', {value: opt.value}, opt.text);
    })
])

This is fully bi-directional so if you set model.selectValue("key2"), the select will update accordingly and as you change the select value, the model selectValue is updated.

Note the use of "text" and "value" in the options, which is standard HTML option attributes, which IMHO is more semantic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants