Knockout.js JSON API Utils
A collection of Knockout.js utils for various tasks related to JSON API standard
Setup
npm installnpm run compile
Usage
Load the file build/index.js
in with your javascript, then:
// Sometime before initializing a `KOFormBase`, `Object.assign` the provided// extenders to `ko.extenders` by calling:KnockoutJsonApiUtils; KOFormBase { // this is called after a successful save (if save_after_edit is defined below) // updated_record is the response from the server } { super; // Set up some observables we'll need later on thissome_stuff = ; thissome_other_stuff = ; // `this.init(opts)` consists of a chain of promises: // -> await parallel execution of requests & nested vm creation // -> parse response to main request & build `VM` (i.e. `this`) by doing: // 1. strip out attributes form response // 2. create `VM[attribute_name]` extended observable (as defined in // Extensions) for each attribute not in // `opts.obseravble_attributes_blacklist` and push into // `VM.observables_list` observableArray // 3. create `VM[relationship_name]` extended observable or // observableArray (as defined in Extensions) for each relationship // specified in `opts.relationships` and push into `VM.relationships` // observableArray // -> await parallel execution of `handleOtherResponses` & `finalizeInit` this; } { // the responses from other_requests above // no need to call super.handleOtherRequests(responses); all that does is returns an empty promise // responses === [response1, response2] (see other_requests above) // If a promise is returned, it will be resolved before `this.finalizeInit()` is called this this; } { // stuff before finalizing super // sets up numErrors, is_valid, etc. // stuff after finalizing // If a promise is returned, it will be resolved before `this.init()` resolves }