AmpersandJS / ampersand-input-view

A view module for intelligently rendering and validating input. Works well with ampersand-form-view.
MIT License
17 stars 19 forks source link

How do I extend the view with new bindings without overwriting the defaults? #1

Closed webmasterkai closed 10 years ago

webmasterkai commented 10 years ago

If I want to extend this view with the purpose of defining some new bindings it seems I must copy and paste the bind gins defined in the view if I want them to persist. With other things like props they get merged in. What is the best way to merge bindings?

HenrikJoreteg commented 10 years ago

This is currently not supported directly. Hm... this feels like it should work, though. Trick is, I just don't want to make a ton of exceptions to what property names get merged, since this happens in ampersand-state. On approach would be to allow the list of properties that are merged to be configurable in state.

secobarbital commented 10 years ago

In the case of extending bindings, it is even more complicated, because we might even want to add to an existing binding, turning the value from a string or object into an array.

We might have to allow subclasses to configure not only which properties to merge but take a function to perform the merge.

For now, my workaround is to extend the prototype manually after extending. This does not solve the problem I mentioned above, which would still require knowledge of the built-in bindings.

InputView.extend({
    props: {
        'id': 'string',
        'prefix': 'string'     
    },
    bindings: _.extend({       
        'prefix': '[data-hook=prefix]', 
        'id': [                
            {                  
                type: 'attribute',              
                name: 'for',   
                hook: 'label'  
            },                 
            {                  
                type: 'attribute',              
                name: 'id',    
                selector: 'input'               
            }
        ]
    }, InputView.prototype.bindings)
});
HenrikJoreteg commented 10 years ago

this discussion is now being carried on here: https://github.com/AmpersandJS/ampersand-view/issues/39 where it's more relevant. Closing this one as a duplicate.