resthub / resthub-backbone-stack

RESThub Backbone.js stack
http://resthub.org/docs/backbone/
Other
56 stars 26 forks source link

_ensureContext do not handle well dynamic context #136

Closed Steph0 closed 11 years ago

Steph0 commented 11 years ago

Hi,

Info : "// RESThub Backbone stack 2.1.2"

I needed to create a dynamic context to a view. So I followed your documentation ( http://resthub.org/backbone-stack.html#default-render-with-root-and-context-attributes ), paragraph about creating a dynamic context.

This works well when I declare it and call later MyView.render() (so render without args. However, we noticed that this dynamic context is not reapplied when render is call with args. Typically, if your decide to do this kind of code :

Resthub.View.extend({
    events: {},
    template: MyTemplate,
    labels: labels.myLabels,
    strategy: "append",
    tagName: "li",
            context: function() {
                        return {
                            custom: {number:5}, // Or anything else, just an example
                            model:   this.model,
                            labels:   this.labels
                        };
            },

    initialize: function() {
                this.listenTo(this.model, "change", this.render);
    },

            render: function() {
                    UploadItemView.__super__.render.apply(this, arguments);

                    return this;
    }
});

When the change event is triggered (for example by a fetch), the dynamic context will not be part of the render.

We detected that this problem occurs because of ensureContext function in resthub.js. In fact when render is called whith an arg, this .each loop ignore the dynamic context :

_.each([this.context, 'model', 'collection', 'labels', 'modelJson', 'collectionJson'], function(key) {
            if (typeof this[key] !== "undefined")
                if (this[key].toJSON) {
                    context[key] = this[key].toJSON();
                } else {
                    context[key] = this[key];
                }

        }, this);

A solution we found, rewriting this _.each like this (we assume that this is maybe not the best manner, but it works) :

_.each([this.context, 'model', 'collection', 'labels', 'modelJson', 'collectionJson', 'viewOptions'], function(key) {
            if (typeof this[key] !== "undefined")
                if (this[key].toJSON) {
                    context[key] = this[key].toJSON();
                } else {
                    context[key] = this[key];
                }

        }, this);

In this, our custom parameters is given in a dynamic context with a "viewOptions" attributes. So as the key viewOptions exists, we can have thus a dynamic context.

Thank you for considering this issue,

Stephen (AWL)

sdeleuze commented 11 years ago

Hi, I begin to work on this issue.

sdeleuze commented 11 years ago

Fixed. Also generalized merge versus replace behaviour for other attributes ...

Steph0 commented 11 years ago

Thanks :+1: