backbone-forms icon indicating copy to clipboard operation
backbone-forms copied to clipboard

Unable to use select editor outside of a form

Open there4 opened this issue 11 years ago • 14 comments

The documentation provides an example of using a select editor outside a form.

var select = new Backbone.Form.editors.Select({
    model: user,
    key: 'country',
    options: getCountries()
}).render();

However, in the select editor on line 29 it requires a schema to initialise. This appears to prevent the example from working correctly. I added a new line to correct this:

initialize: function(options) {
  editors.Base.prototype.initialize.call(this, options);
  if (!_.isUndefined(options.options)) {
    this.setOptions(options.options);
  }
  else if (!this.schema || !this.schema.options) throw "Missing required 'schema.options'";
}

But I wasn't sure if I'd misunderstood something. I'm more than happy to send a pull request and unit test. But, I wanted someone from the project to check out this behaviour before I did that.

Am I missing something?

there4 avatar Apr 06 '13 13:04 there4

Probably best to remove the need for the schema entirely. If the options aren't defined then the Select can be empty. That should fix this issue and allow you use the select separately.

powmedia avatar Apr 07 '13 01:04 powmedia

Would you like me to do this and send a pull request?

there4 avatar Apr 07 '13 12:04 there4

Yep that would be great, just make sure the unit tests pass and that you've got the latest code from master as it's all been updated recently. Thanks

powmedia avatar Apr 07 '13 15:04 powmedia

Do you have a .jshintrc file that you use with this project?

there4 avatar Apr 07 '13 16:04 there4

One more question - it appears that both checkboxes and radio have this same behaviour from extending Form.editors.Select. They have unit tests to ensure that they throw errors when init'd without a schema.

Would you like for them to continue to throw errors, or should they have this same setOptions, and no schema?

there4 avatar Apr 07 '13 16:04 there4

They can act the same as the Select

powmedia avatar Apr 07 '13 16:04 powmedia

Alright, I'll remove those unit tests and send this along.

Do you have any interesting in outfitting this project with Grunt as a build/test system and hooking it up to TravisCI? I'd pitch in for that project.

there4 avatar Apr 07 '13 18:04 there4

Possibly; would be good to have integration with Travis however when I briefly looked at Grunt it looked a bit overcomplicated to set up. Do you have an example of Grunt being used in a similar way to how it would be used here?

powmedia avatar Apr 07 '13 18:04 powmedia

You could check out the Grunt Init template for jquery projects, it's pretty stripped down.

How about this - I'll make a branch and just wire it up sometime this week and you can see if you like it. It would be pretty minimal and would supply jshint, build, test, and a watch. Having Travis included would make it easy for you to confirm that a pull request doesn't break anything.

I've really enjoyed this Forms project, and would like to contribute this to it. Thoughts?

there4 avatar Apr 07 '13 18:04 there4

I started on a Grunt setup with backbone-forms here: https://github.com/powmedia/backbone-forms/pull/200 With the purpose of being able to setup Travis with it.

It just had a few focus() related tests failing, and now needs to be updated with the changes from 2.1 before being good-to-go.


philfreo avatar Apr 07 '13 19:04 philfreo

The was focus() was working in tests has changed with the new version, it might fix those issues

On 7 Apr 2013, at 12:11, Phil Freo [email protected] wrote:

I started on a Grunt setup with backbone-forms here: https://github.com/powmedia/backbone-forms/pull/200 With the purpose of being able to setup Travis with it.

It just had a few focus() related tests failing, and now needs to be updated with the changes from 2.1 before being good-to-go.


— Reply to this email directly or view it on GitHub.

powmedia avatar Apr 07 '13 19:04 powmedia

Ah, excellent. Those testing changes look really promising. I was just looking into the wrap that happens with the current build system. I've got a grunt file here that is /almost/ ready to replace the current build tool. I'd noticed the failing tests under Phantom, but I hadn't looked to address those. Sinon really is a great tool.

there4 avatar Apr 07 '13 20:04 there4

If I'm reading this correctly it appears this issue isn't resolved? if so, I tried setting up a select editor outside a form and I get the "Missing required 'schema.options'" message my code look like the following: var select = new Backbone.Form.editors.Select({ key: "id", model: localModel, options: new userCollection() })

also tried setting options to "" and ["1", "2", "3"].

AceOfSpades29 avatar Apr 06 '17 15:04 AceOfSpades29

For me following did the trick:

var select = new Backbone.Form.editors.Select({
    schema: {
        key: 'country',
        options: collection,
        title: 'Countries',
        validators: [],
        editorClass: '',
        fieldClass: '',
    }
});
$('body').append(select.render().el);

quayle avatar May 15 '21 21:05 quayle