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

Support plain JS and ko-es5 models #55

Merged
merged 9 commits into from
Jun 23, 2018

Commits on Jun 20, 2018

  1. POJO-Support: Compel knockout to generate _ko_property_writers for a …

    …few more of its bindings, by marking them as two-way.
    
    Some of knockout's built-in bindings (checked, hasFocus, selectedOptions, textInput, and value) are already listed as "two-way bindings" by default. For these, knockout generates an additional binding named "_ko_property_writers" which exposes functions for writing these values to non-observable model properties.
    
    We need to tell knockout to generate property writers for more of its built-in bindings, so that `init` can use these to write to the model.
    revengineering committed Jun 20, 2018
    Configuration menu
    Copy the full SHA
    920979c View commit details
    Browse the repository at this point in the history
  2. POJO-Support: Added utility method for creating a binding preprocesso…

    …r that generates custom property writers.
    
    Knockout's _twoWayBindings collection only covers basic `name: value` bindings.
    For more complex bindings that receive an object literal (including KO's own "attr"), we need to generate property writers ourselves.
    revengineering committed Jun 20, 2018
    Configuration menu
    Copy the full SHA
    6d9e532 View commit details
    Browse the repository at this point in the history
  3. POJO-Support: Modified init binding, to use property writers for no…

    …n-observable model properties.
    
    This commit expands on the existing logic of finding and invoking fieldAccessors. But if no fieldAccessor is found, or it is not observable, then look for a suitable property writer instead.
    revengineering committed Jun 20, 2018
    Configuration menu
    Copy the full SHA
    e09e8db View commit details
    Browse the repository at this point in the history
  4. POJO-Support: Modified foreachInit binding, so it can work with pla…

    …in arrays, and also subscribe to changes in a knockout-es5 tracked array property.
    
    The trickiest part of this commit is setting up the subscription with change tracking. When the array comes from a knockout-es5 "tracked" model, it is actually being read from an ES5 property getter which reads from an internal observableArray. But we don't have access to that underlying observableArray here, so we can't call `array.subscribe(..., "arrayChange")`.
    
    Instead, we take advantage of Knockout's dependency tracking and set up a `ko.computed` which reads the array from `valueAccessor()`. This sets up a dependency such that the body of our `computed` will be invoked any time the array changes. This works regardless of whether the model uses an actual observableArray or a ko-es5 tracked array property.
    
    Since we can no longer reliably subscribe to the "arrayChange" notification to get the diffs, we need to take on a bit of that responsibility ourselves (basically doing the same as what KO would do if we did subscribe).  We keep track of the previous array contents, and then use `ko.utils.compareArrays` to get the differences.
    
    (I had to move the biniding's `init` logic into the `InitializedForEach` constructor so that the `ko.computed` could use the `valueAccessor()`)
    revengineering committed Jun 20, 2018
    Configuration menu
    Copy the full SHA
    e12f6bb View commit details
    Browse the repository at this point in the history

Commits on Jun 22, 2018

  1. POJO-Support: Added tests.

       * Added knockout-es5 package - This is only required for test and some examples, but is entirely optional when using knockout-pre-render.
       * Added tests against plain JS models.
       * Added tests against knockout-es5 tracked models
    revengineering committed Jun 22, 2018
    Configuration menu
    Copy the full SHA
    933d546 View commit details
    Browse the repository at this point in the history
  2. POJO-Support: Added examples.

       * Example that populates a plain JS model.
       * Example that populates a knockout-es5 model
    revengineering committed Jun 22, 2018
    Configuration menu
    Copy the full SHA
    0146a3b View commit details
    Browse the repository at this point in the history
  3. POJO-Support: Made array subscription style conditional.

       * If the incoming data is observable, subscribe to its changes the normal (simpler) way.
       * If it's *not* observable, then use the "computed" approach with explicit change tracking.
    
    This avoids redundant change tracking in the case the observableArray has multiple subscribers to the 'arrayChange' notification.
    revengineering committed Jun 22, 2018
    Configuration menu
    Copy the full SHA
    df32fc1 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    cd61907 View commit details
    Browse the repository at this point in the history
  5. POJO-Support: Styling and comment fixes.

       * Removed unnecessary parens around some boolean expressions.
       * Fixed an error in a comment.
    revengineering committed Jun 22, 2018
    Configuration menu
    Copy the full SHA
    1ef7134 View commit details
    Browse the repository at this point in the history