Friday, 15 August 2014

javascript - Attempting to loop through files with Q.js -



javascript - Attempting to loop through files with Q.js -

i attempting utilize q.js handle issues promises , deferrable in web application - start off saying understanding of async 0 right now. first time i've tried it, , really lost after reading much of documentation can.

i previously using library called jsdeferred accomplish next code; loops through list of files , loads them, , adds them array. have since learned should not utilize jsdeferred , told should instead utilize promises , deferrables correctly.

i have explored lot of venues , may stupid, having hard time implementing exact code in promises oriented library (in example, trying utilize q.js, no success).

q.js library define(function () { homecoming function (selector, callback) { var files = [ "/app_content/json/ecma5.json", "/app_content/json/jquery.json", "/app_content/json/tangent.json" ]; var results = []; var editor, server; homecoming deferred.loop(files.length, function (i) { homecoming $.get(files[i]).next(function(data) { results.push(data); }); }).next(function () { // lot of things happen here. amazing things. }).next(function() { // seriously, stuff awesome. }).next(function() { callback(editor); }); }; });

i'm having hard time file loading/looping, help appreciated. think 1 time footing here, i'll able proceed lot better, file looping throwing me off. maintain reading in documentation seems one-time utilize scenarios.

i still reading documentation, , go on so, if can help me footing here appreciate it. 1 time see working of own, it's easier me pick other situations. have 20 other places need start using concept, first 1 giving me headache.

update

i not have utilize q.js, 1 came recommended. looking @ https://github.com/caolan/async if solve problem.

further update

working more docs, have amalgamated of working code, still seems missing something. having problem passing results parameters each then(fn), have maintain outside variable.

var results = []; var editor, server; var chain = files.reduce(function (previous, item) { homecoming previous.then(function(previousvalue) { homecoming q.resolve($.get(item, function(data) { results.push(data); })); }); }, q.resolve()); chain .then(function (results) { }) .then(function (results) { // can't seem results pass through 2nd 'next'. }) .then(function () { callback(editor); }); final result

with help of here, have made code work how want. end result. implementation of codemirror using tern , custom script definitions.

define(function () { homecoming function (selector, callback) { var editor, server, results, files = [ "/app_content/json/ecma5.json", "/app_content/json/jquery.json", "/app_content/json/tangent.json" ]; q .all(files.map($.get)) .then(function(data) { results = data; }) .then(function() { editor = codemirror.fromtextarea(selector[0], { mode: { name: "javascript", globalvars: true }, linenumbers: true, linewrapping: true, matchbrackets: true, indentunit: 2, tabmode: "spaces", autoclosebrackets: true, matchtags: true, highlightselectionmatches: true, continuecomments: "enter", foldgutter: true, width: "100%", gutters: ["codemirror-linenumbers", "codemirror-foldgutter"], extrakeys: { "ctrl-space": "autocomplete", "ctrl-q": function(cm) { cm.foldcode(cm.getcursor()); } } }); }) .then(function() { server = new codemirror.ternserver({ defs: results }); editor.setoption("extrakeys", { "ctrl-space": function(cm) { server.complete(cm); }, "ctrl-i": function(cm) { server.showtype(cm); }, "alt-.": function(cm) { server.jumptodef(cm); }, "alt-,": function(cm) { server.jumpback(cm); }, "ctrl-q": function(cm) { server.rename(cm); }, }); editor.on("cursoractivity", function(cm) { server.updatearghints(cm); }); }) .then(function() { callback(editor); }) .done(); }; });

i offer extreme, extreme constructive, helpful, useful, , knowledgeable info provided here.

using q.js, , assuming not need send info along request, shorten code using map , q.all:

var results, files = [ "/app_content/json/ecma5.json", "/app_content/json/jquery.json", "/app_content/json/tangent.json" ]; q.all(files.map($.get)) .then(function(_results) { results = _results; }) .then(function () { // more awesome stuff here }) .then(function () { // etc... console.log(results); }) .done();

note in order utilize results within subsequent .then() blocks, must save off reference outside of promise chain. in example, results wanted local function passed in then() - shadowed global results. give different name, _results, , assign global results in order able utilize later.

javascript jquery asynchronous q

No comments:

Post a Comment