Skip to content

Instantly share code, notes, and snippets.

@frequent
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frequent/9385903 to your computer and use it in GitHub Desktop.
Save frequent/9385903 to your computer and use it in GitHub Desktop.
Retrieving Networks - comparing current and offline version
/*
* called inside app-code file
*/
// custom code for "networks"
"networks": function (reply) {
var config, property, query, href, i, reply, uri, pass = reply.pass;
if (storage) {
// =========================================================
// 1. first request to retrieve "root" to have access to "me"
return jIO.util.ajax({
"url": storage.items.dict.url,
"type": "GET",
"xhrFields": {
"withCredentials": true
})
.then(function(response) {
var parsed = util.parse(response.target.responseText),
me = parsed._links.me,
...
// test for authentication
if (!me) {
return {"pass": pass};
} else {
uri = new URI(me.href)
// =========================================================
// 2. second request to get "me", which contains action/queries
// I must call for ERP5storage
return storage.items.get({"_id": uri.segment(2)});
}
}).then(function (answer) {
if (answer.pass) {
return {"pass": pass};
} else {
// =========================================================
// 3. retrieve action/query from "me"
reply = util.parse(answer.data)._links.slapos_jump;
for (i = 0; i < reply.length; i += 1) {
if (reply[i].name === "current_network") {
query = reply[i]._query;
}
}
// =========================================================
// 4. overwrite default query with new query and send it back
// into the content loop, where the query will be made (below)
if (query === undefined) {
query = reply.href;
}
if (pass.config.initial_query === undefined) {
pass.config.initial_query = {};
}
pass.config.initial_query.query = query;
// pass hacked query back into chain
return {
"response": util.parse(reply.response),
"pass": pass
};
}
}).fail(util.error);
}
return (reply);
},
...
app.fetchData = function (parcel) {
var method, convert, select_list, hacked_view, pass, skip, query;
pass = parcel.pass;
query = parcel.query;
...
select_list = parcel.query && parcel.query.select_list &&
parcel.query.select_list.length;
if (parcel.query._id) {
hacked_view = {"_view": parcel.pass.config.view}
method = "get";
}
// =========================================================
// 5. here ALLDOCS is called on ERP5 storage
return storage[parcel.storage][method || "allDocs"](parcel.query, hacked_view)
.then(function (response) {
...
// map response and return
});
};
/*
* inside ERP5storage
*/
ERP5Storage.prototype.allDocs = function (command, param, options) {
// =========================================================
// 6. get "root" again
return this._getSiteDocument()
.then(function (site_hal) {
// =========================================================
// 7. pick search URL from "root" and run the query passed
return jIO.util.ajax({
"type": "GET",
"url": UriTemplate.parse(site_hal._links.raw_search.href)
.expand({
query: options.query,
// XXX Force erp5 to return embedded document
select_list: options.select_list || ["title", "reference"],
limit: options.limit
}),
"xhrFields": {
withCredentials: true
}
});
})
.then(function (response) {
return JSON.parse(response.target.responseText);
})
.then(function (catalog_json) {
// =========================================================
// 8. construct response
....
})
.fail(function (error) {});
};
/*
* So there are 2 requests ("root", "me") inside the app-code which
* retrieve the query to run inside ERP5storage.
*
* Inside ERP5storage, "root" is requested again to retrieve the
* ERP5 query, overwrite the default query.
*
* Note in this example original query and query for ERP5 are the
* same ("portal-type:network")
*
* To make something work offline quickly, I moved the ERP5storage
* custom requests and queries into ERP5 storage, so I can call
* allDocs() in the app-code independent of the used storage =
* for localStorage/ERP5storage/any. Also this makes all custom
* handlers like "network" no longer necessary inside app-code
*/
/*
* Now looks like this inside app-code:
*/
storage.fetch = function (obj) {
var pass, query, method, map, config, answer, lookup, pointer, action_id,
quirk;
pass = obj.pass;
quirk = obj.state || pass.config;
lookup = obj.state || quirk.property_dict || storage.obj;
query = obj.query || lookup.query || storage.obj;
pointer = (lookup["url_pointer"] || storage.obj).jump;
switch (true) {
case (query.select_list.length > 0 && !query.include_docs):
...
break;
case (!!query._id):
...
method = "get";
config = {
"_view": quirk.view
};
break;
}
// =========================================================
// 1. pointer = "current_network" set inside my JSON "form-definition"
// Pass pointer as parameter to ERP5storage. Will be ignored by other
// storages (unknown parameter)
if (pointer && !config) {
config = {
"_jump": pointer
};
}
// =========================================================
// 2. call allDocs directly
return RSVP.resolve(
app.storage_dict.items[method || "allDocs"](query, config)
).then(function (response) {
... map response
}).fail(util.error);
},
/*
* inside ERP5storage, the allDocs then is
*/
ERP5Storage.prototype.allDocs = function (command, param, options) {
var that = this, search_pointer;
// =========================================================
// 3. get "root", previously done in app-code/here
return that._getBase()
.then(function (site_hal) {
search_pointer = site_hal._links.raw_search.href;
// =========================================================
// 4. get "me", previously done in app-code
return that._getDoc(command, site_hal, param, options);
})
.then(function (opts) {
// =========================================================
// 5. retrieve the passed _jump parameter to lookup query to run
if (param._jump && (options.query || options._id)) {
options.query = that._getParam(opts._links.slapos_jump, param._jump);
}
// =========================================================
// 6. make the query, using _getBase again
return that._getBase(
search_pointer,
{
"query": options.query,
// XXX Force erp5 to return embedded document
"select_list": options.select_list || ["title", "reference"],
"limit": options.limit
}
);
})
.then(function (catalog_json) {
... map response
})
.fail(function (error) {});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment