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

make 'with' more lightweight #9

Open
mbest opened this issue Dec 20, 2011 · 21 comments
Open

make 'with' more lightweight #9

mbest opened this issue Dec 20, 2011 · 21 comments
Assignees

Comments

@mbest
Copy link
Owner

mbest commented Dec 20, 2011

The 'with' binding currently uses the native template template engine, which adds a lot of overhead. The main purpose of the 'with' binding is to push a new binding context to the stack for child elements (doesn't affect any other bindings on the same element as 'with').

The current implementation also does an 'if' with the target of 'with', which I think is incorrect (doesn't match the way 'with' works in JS or in other languages). So although removing the templating code under with would remove the 'if' functionality, I thinks that it's better anyway.

'with' should still work in container-less bindings (), which would need to be modified to work with non-template bindings.

@mbest
Copy link
Owner Author

mbest commented Jan 6, 2012

ko.bindingHandlers['withlight'] = {
    'init': function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var bindingValue = valueAccessor();
        if (typeof bindingValue != 'object' || bindingValue === null)
            throw new Error('withlight must be used with an object');
        var innerContext = bindingContext['createChildContext'](bindingValue);
        ko.applyBindingsToDescendants(innerContext, element);
        return { 'controlsDescendantBindings': true };
    }
};

@mbest
Copy link
Owner Author

mbest commented Jan 6, 2012

Oops. Last commit also included some unrelated changes to dependentObservable.

@ghost ghost assigned mbest Jan 6, 2012
@dotnetwise
Copy link

Any chance I can use this with containerless bindings?

....

It's pretty hard to generate

  • elements without a container otherwise.

    The "with binding" sucks, at it replaces the html itself, thus all the pre-filled values, jquery event bindings etc. will be lost!

    @mbest
    Copy link
    Owner Author

    mbest commented Jan 20, 2012

    Not at this point. It would require some changes within Knockout.

    @dotnetwise
    Copy link

    Too bad :(
    I guess withlight should be a mandatory feature of ko
    Since "with" breaks any javascript plugin that would be rendered within an comment "with" binding
    And that's because ko replaces the DOM thus all the content, event bindings and custom jquery data is lost.

    "with" makes ko unusable. It should for sure not "recreate" the DOM!

    @dotnetwise
    Copy link

    Why not updating KO itself?

    @mbest
    Copy link
    Owner Author

    mbest commented Jan 23, 2012

    I've submitted a pull request for Knockout to support custom container-less binding: knockout#290

    @dotnetwise
    Copy link

    Where did the withlight binding go in the latest v2.1.0pre?

    @mbest
    Copy link
    Owner Author

    mbest commented Mar 5, 2012

    I don't think it was ever included in 2.1. I'm hoping we'll get it in as part of 2.2.

    @vamp
    Copy link

    vamp commented Mar 31, 2012

    maybe withlite can be renamed to "using"?

    @NoelAbrahams
    Copy link

    Please can we have this included in KO? This is a core feature and maintaining this as a custom binding outside of KO can introduce bugs.

    I also vote that "using" is an appropriate name for this binding, and sounds better than "withlight", because rather than being a lighter version of "with" it is really a replacement for it. This naming would permit phasing out of "with".

    @mbest
    Copy link
    Owner Author

    mbest commented Jul 7, 2012

    I've opened an issue for the improved with: knockout#476. Feel free to add your comments there also. Ultimately, it's Steve who decides what gets included in Knockout.

    @dotnetwise
    Copy link

    So what's the current status?
    I can't seem to be finding anything out of the box for 2.1 in the docs

    @mbest
    Copy link
    Owner Author

    mbest commented Sep 24, 2012

    So what's the current status?

    We're planning to include an improved with in the upcoming version of Knockout, 2.2.

    @mbest
    Copy link
    Owner Author

    mbest commented Oct 30, 2012

    Knockout 2.2 includes a new implementation of with that does what withlight does (and everything that with did before). Check it out.

    @dotnetwise
    Copy link

    Already in the docs?

    @mbest
    Copy link
    Owner Author

    mbest commented Oct 30, 2012

    We didn't update the docs for it since it's not exactly a feature. It just makes the binding work more like people expect.

    @dotnetwise
    Copy link

    Will it appear in the docs? :)

    @dotnetwise
    Copy link

    The above withlight works great, but it still is missing from the latest KO.
    For example, if you are using with: loginData on your login form then it will break your browser's autocomplete feature for the username/password.
    However with the withlight binding it works as exepcted.
    So any chance you'd add it to the main branch as well?

    @mbest
    Copy link
    Owner Author

    mbest commented Jun 11, 2013

    I'd like to see what you're talking about. Can you put together an example for me?

    @dotnetwise
    Copy link

    Do you konw Durandal ? Imagine the login form is just a module that gets loaded dynamically.
    Of course password manager won't work.
    But, if you move the login form in the main html itself, then there are two cases:

    • with - will rewrite the markup, so you'll loose the autocompleted username/password
    • withlight - will work perfectly :)

    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

    4 participants