1. #1
    Sencha User castitas's Avatar
    Join Date
    Sep 2011
    Location
    US
    Posts
    116
    Answers
    3
    Vote Rating
    3
    castitas is on a distinguished road

      0  

    Default Answered: Proxy on model and store

    Answered: Proxy on model and store


    What would happen if both a store and a model have a proxy?

    Why:
    Store: A, B
    Model: M

    A and B both use M, but A has some specific requirements/parameters that are most easily specified in a proxy.
    word

  2. I see this line in the constructor for AbstractStore:

    Code:
    me.setProxy(me.proxy || me.model.getProxy());
    It suggests that the proxy on the store will take precedence. Surely the easiest way to be sure is just to try it?

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,596
    Answers
    542
    Vote Rating
    324
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    I see this line in the constructor for AbstractStore:

    Code:
    me.setProxy(me.proxy || me.model.getProxy());
    It suggests that the proxy on the store will take precedence. Surely the easiest way to be sure is just to try it?

  4. #3
    Sencha User castitas's Avatar
    Join Date
    Sep 2011
    Location
    US
    Posts
    116
    Answers
    3
    Vote Rating
    3
    castitas is on a distinguished road

      0  

    Default Correct!

    Correct!


    I experimented and you're right (Turns out experimenting didn't involve as much work as I thought it did).

    I'm a little curious as to the code, though. I thought a boolean operator (||) would return a boolean value.
    word

  5. #4
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,596
    Answers
    542
    Vote Rating
    324
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Using the || operator like that is a pretty common paradigm in JavaScript. I'll attempt to explain...

    As you're probably aware, JavaScript has implicit type conversion. When a boolean is required, some values correspond to true and some to false. These are commonly referred to as truthy and falsey values respectively. false, 0, "", NaN, null and undefined are all falsey.

    The || operator will return the left-hand argument if the left-hand argument is truthy. Otherwise it will return the right-hand argument. Something a bit like this:

    Code:
    function OR(a, b) {
        if (a) {
            return a;
        }
    
        return b;
    }
    It does not convert the return value to a boolean as part of this process. If you draw up a truth table, or just stare at it long enough, you should eventually be able to convince yourself that this way of evaluating || actually does correspond to an OR operator. If either input value is truthy then the output will also be truthy. Only if both values are falsey will the output be falsey.

    So, for example, if you write this:

    Code:
    var c = 0 || null;
    Both arguments are falsey. c will be null, the right-hand argument, which is also falsey.

    As another example, consider:

    Code:
    var d = null || 'hello';
    Here the left-hand argument is falsey but the right-hand argument is truthy. d will end up being 'hello', which is truthy.

    A consequence of this implementation of || is that it can be used to provide default values. This is what is happening in the example with the proxy. Again, if this isn't obvious then stare at it, break out a pen and paper and convince yourself that this is true. Consider what a default value function might look like, you'll probably end up with something similar to the OR function I wrote earlier.

    The && operator is similar. You could think of it as follows:

    Code:
    function AND(a, b) {
        if (a) {
            return b;
        }
    
        return a;
    }
    The && operator can also be used to do trickery, just like the || operator, but it is much less common.

    Hope that helps.

  6. #5
    Sencha User castitas's Avatar
    Join Date
    Sep 2011
    Location
    US
    Posts
    116
    Answers
    3
    Vote Rating
    3
    castitas is on a distinguished road

      0  

    Default


    It does, thank you. You did a wonderful job of explaining.
    word

Thread Participants: 1

Tags for this Thread