Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI hash problems with Backbone Marionette Layout

In the following Layout, I am adding a CollectionView to display a SELECT list within onRender. Immediately after that, I am using the ui hash to enable or disable all controls within the view. This does not work for the SELECT generated by new App.View.Categories.

Should it? Or does the UI hash not work on Regions within a Layout?

App.View.UploadFile = Backbone.Marionette.Layout.extend({
    template: '#upload-file-template',
    regions:{
        category: 'td:nth-child(4)'
    },
    ui:{
        inputs: 'textarea, select, .save'
    },
    onRender: function(){
        this.category.show(
            new App.View.Categories({
                collection: App.collection.categories
            }) // generates the SELECT list
        );

        console.log(this.ui.inputs); // Length 2. Missing select.
        console.log(this.$('textarea, select, .save')); // Length 3

        this.ui.inputs.prop(
            'disabled', (this.model.get('upload_status')!='staged')
        );
    }
});
like image 400
Bart Avatar asked Feb 12 '13 17:02

Bart


People also ask

What is marionette backbone?

Marionette simplifies your Backbone application code with robust views and architecture solutions. Organize your app in terms of small Views. Marionette makes it easy to compose rich layouts out of small components Show a sorted filtered list without breaking a sweat. Have a massive collection?

What is a marionette object?

Marionette Objects support features like extend, events, initialize, and more. Why Marionette? Confused about how something works? Read the annotated source code. Think of Marionette as the code you would have written if you had the time and a thousand sets of eyes.

Why read the marionette source code?

Read the annotated source code. Think of Marionette as the code you would have written if you had the time and a thousand sets of eyes. Marionette community is home to the most welcoming and vibrant discussions in the Backbone ecosystem. Have a large unruly code base that you can not simply rewrite? Marionette can be added in pieces.

What are the benefits of marionette?

Share complex UI interactions across views. Behaviors are like mixins, without all of the pain associated with property collision. Decoupled communication between your application components with a powerful messaging system Write classes with the same API as your views. Marionette Objects support features like extend, events, initialize, and more.


1 Answers

This should be working the way you expect it to work. The code in question in the Marionette source is here: https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.itemview.js#L49-L51

The call to bindUIElements() is what converts the ui hash in to jQuery selector objects, and it is called right before the onRender method is called.

Are you seeing errors? Or is the selector simply returning nothing, and having no affect on the elements?


Update:

Ah! Of course... I wasn't paying attention to your code close enough. You're correct in that the UI element selectors happen before you're adding the the sub-view to the region. I've never run in to this situation before... but this seems like something we would want to fix / support.

For now, the best workaround I can suggest would be to call 'this.bindUIElements();' at the very end of your onRender method. This would force the ui elements to re-bind to the selectors.

I'll also add an issue to the github issues list, to look in to a better solution for this. i don't know when i'll be able to get to this, but this will at least get it on the list of things to fix.

like image 175
Derick Bailey Avatar answered Nov 13 '22 20:11

Derick Bailey