Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_ensureContext do not handle well dynamic context #136

Closed
Steph0 opened this issue Jul 4, 2013 · 3 comments
Closed

_ensureContext do not handle well dynamic context #136

Steph0 opened this issue Jul 4, 2013 · 3 comments
Milestone

Comments

@Steph0
Copy link

Steph0 commented Jul 4, 2013

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
Copy link
Member

Hi, I begin to work on this issue.

@sdeleuze
Copy link
Member

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

@Steph0
Copy link
Author

Steph0 commented Jul 10, 2013

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants