powmedia/backbone-forms — Issue #28: Design advice: specifying data source for an autocomplete editor
Author: backspace
Created: 2012-01-26 18:28:41+00:00
State: open

Thanks for this library.

I’m making use of it in a project, and I extended `Backbone.Form.editors.Base` to create an `Autocomplete` editor that uses jQuery UI’s autocomplete. Usage example:

```coffeescript
class Move extends Backbone.Model
schema:
person_id:
title: "Person"
type: "Autocomplete"
collection: "Roster.people"
```

It works nicely, and I’m hoping to be able to contribute this back to the community (tests already written), but there’s a design flaw: the mechanism of specifying the data source.

I originally passed the collection in directly, but then I ran into loading order problems. Now it uses the dreaded `eval` to find the collection. It’s okay for my purposes, but wouldn’t be happy releasing it that way for general usage. Apart from being a poor solution, it’s also limited in that there can only be one backing collection, so I can’t use, say, a subset of the `People` collection if I need to.

Does anyone have thoughts on how I can make a clean interface so other people can integrate this type of editor? Is it just not a good fit for the design of Backbone Forms? Here are ideas I’ve considered and why I think they don’t work:

* passing in a function for the data source: editor could then only find objects in global scope
* specifying a `datasource` or something that could be passed in when constructing the `Form`: an `Editor` has no access to the `Form` that contains it

An extra benefit for me to solving this would be that I can add validation that depends on an entered value being in a collection determined at runtime.

I debated whether this was the venue for this sort of question or whether Stack Overflow was better. Sorry if I chose incorrectly.