Lawnchair Phonegap Iteration Script - javascript

I am using Lawnchair for HTML5 iOS App Development for Persistant Storage.
I have an iteration script that returns undefined 11 times, here is the script
var lawnchair = Lawnchair({name:'relaxed', record:'config'}, function(people){
this.all(function(record, index) {
records = record.length;
couchURL = record.couchURL;
alert(records);
for(i = 0, len = records; i < len; i++){
console.log(couchURL);
}
})
})
The Data is obviously in local storage but isn't being iterated out correctly, could anybody provide a fix for this?
Thanks

Try this :
var lawnchair = Lawnchair({name:'relaxed', adapter:'dom'}, function(people){
})
this.all(function(data) {
for(var i = 0; i<data.length;i++)
{
Get data using this...data[i].value.fieldname
}
})

Related

JavaScript - push results from for loop into array (with SoundCloud API)

I have this function that iterates through a user's favourited track genres on SoundCloud, and then I want to push these results into an array however am having problems. Any suggestions?
SC.connect(function() {
SC.get('/me/favorites', function(favorites) {
console.log(favorites)
for (i = 0; i < favorites.length; i++) {
console.log(favorites[i].genre);
favorites[i].genre.push(meGenrePref);
}
var meGenrePref = [];
console.log(meGenrePref);
});
});
I'm not expert on SoundCloud APIs but watching your code I thought this solution:
var meGenrePref = [];
SC.connect(function(){
SC.get('/me/favorites',function(favorites) {
console.log(favorites);
for(var i = 0; i < favorites.length; i++)
{
console.log(favorites[i].genre);
meGenrePref.push(favorites[i].genre);
}
console.log(meGenrePref);
});
});
You were trying to push the contents to a wrong variable, which was the response from the API.
In this solution we are saving to meGenrePref all genres.

Extending a google spreadsheet into a google web app

I was coding using the google scripts, when I came across a problem I've been struggling with for a couple days now. I am using Code.gs (a default page in creating a web app in google), when I called in data from a google spreadsheet to try and display it on a webpage. I had no problems with calling in the data add storing it into a array but now I am struggling with trying to return it to my javascript code. Can this be done or is there something else I can do to fix it? My code is below.
function getContents()
{
var sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xum5t4a83CjoU4EfGd50f4ef885F00d0erAvUYX0JAU/edit#gid=0&vpid=A1');
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
var education = [];
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
if(j==1){
education[education.length] = values[i][j];
}
}
}
}
Logger.log(education);
return education;
}
From that Code.gs code i want it to return it to a Javascript function that says this:
function onNew(){
var input = google.script.run.getContents();
for(var = 0; i<input.length; i++)
{
$("#main").append("<div id='class'>"+input[i]+"</div>);
}
}
and whenever I try to run it says that it causes an error because it is undefined. Thanks in advance! Anything helps!
You need to use the withSuccessHandler(). Your variable input will not receive the return from google.script.run.getContents()
Separate out the client side code into two functions:
HTML Script
function onNew() {
google.script.run
.withSuccessHandler(appendEducationHTML)
.getContents();
};
function appendEducationHTML(returnedInfo) {
console.log('returnedInfo type is: ' + typeof returnedInfo);
console.log('returnedInfo: ' + returnedInfo);
//If return is a string. Convert it back into an array
//returnedInfo = returnedInfo.split(",");
for (var = 0;i < returnedInfo.length; i++) {
$("#main").append("<div id='class'>"+returnedInfo[i]+"</div>);
};
};

AngularJS - Is controller code executed line by line?

I'm new to AngularJS, and is experimenting AngularJS with Twitch API.
I have a list of channels that I'm interested in, defined as var channels.
Then I use the $http.get function to loop through another array, twitchList.channels, which contains the API addresses that I'm supposed to call.
(function() {
var app = angular.module('twitchList', []);
app.controller('twitchController', ['$http', function($http){
var twitchList = this;
twitchList.channels = [];
var channels = ["freecodecamp", "storbeck", "terakilobyte", "habathcx","RobotCaleb","thomasballinger","noobs2ninjas","beohoff", "MedryBW"];
for (var i = 0; i < channels.length; i++ ) {
twitchList.channels.push({
name: channels[i],
api: 'https://api.twitch.tv/kraken/streams/' + channels[i],
})
}
var data_list = [];
for (var j = 0; j < twitchList.channels.length; j++) {
$http.get(twitchList.channels[j].api).success(function(data){
data_list.push(data);
})
}
// Issue arises here!
console.log(data_list);
console.log(data_list.length);
}]);
})();
The API calls seems to be working perfectly, however, I need to get the results of the API call into an array, called data_list. Now, when I print data_list, and data_list.length, what happens is that data_list.length always returns 0, and data_list is sometimes populated (meaning it's either 0 size array or 9 size array). Even though the property of the array has a length 9, but calling .length always gives 0.
This let me think that the controller code is not executed line by line? Or is there something wrong with my logic?
Can someone give me a pointer? Thanks
No, this line:
data_list.push(data);
will be executed when you receive a response on the http request sent a line above. Hence the following lines:
console.log(data_list);
console.log(data_list.length);
will output [] and 0
I've not used it before, but could you possibly use $q.all in order to resolve multiple promises? I've used the equivalent $.when function in jQuery to achieve this in the past.
var data_list = [];
var promise_array = [];
var request;
for (var j = 0; j < twitchList.channels.length; j++) {
request = $http.get(twitchList.channels[j].api);
request.success(function(data) {
data_list.push(data);
});
promise_array.push(request);
}
$q.all(promise_array).then( function() {
console.log(data_list);
console.log(data_list.length);
});

Need Help to adding new array into JSON object

I'm making an file managing web page with nodejs and need some help.
I'm adding some informations into JSON object which recieved from DB.
the DB constains stage informations and I should link informations about stage files.
this is code for the function. Added comments into codes.
DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
for (var i = 0; i < data.stage.length; i++) {
// this was successful. I added an array to check each files.
data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/");
data.stage[i].packVersionArray = data.stage[i].packVersion.split("/");
// this is a problem. I will add file information for each file.
// I
for (var j = 0; j < data.stage[i].packFileNameArray.length; j++) {
fs.stat(dirPath + data.stage[i].packFileNameArray[j], function (err, fileStat) {
if (err) {
// this was first try. I thought the array will be automaticallt created.
data.stage[i].isExist[j] = 'NO';
data.stage[i].mTime[j] = '0';
data.stage[i].size[j] = '0';
} else {
// this was second try. I tried push into each time.
data.stage[i].isExist.push('YES');
data.stage[i].mTime.push(fileStat.mTime);
data.stage[i].size.push(fileStat.size);
}
console.log(data.stage[i].isExist[j]);
console.log(data.stage[i].mTime[j]);
console.log(data.stage[i].size[j]);
});
}
}
});
I want to know how I can add additional informations as an array into the JSON object.
Thank you.
data.stage[i].push({ isExist: 'Yes'});
etc
You have to create the arrays (as the do not seem to exist) before you can push data into them:
DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
for (var i = 0; i < data.stage.length; i++) {
// this was successful. I added an array to check each files.
data.stage[i].packFileNameArray = data.stage[i].packFileName.split("/");
data.stage[i].packVersionArray = data.stage[i].packVersion.split("/");
data.stage[i].isExist = [];
data.stage[i].mTime = [];
data.stage[i].size = [];
//...

List current user's playlists and tracks on spotify

I'm trying to list all the playlists (and their tracks) of the current user on a Spotify integrated app using the API as explains in https://github.com/spotify/apps-tutorial/tree/1.0 .
Is there any way to do this? Or is not supported by now?
Using the 1.X API I did this:
var returnedLibrary;
require(['$api/library#Library'], function(Library) {
returnedLibrary = Library.forCurrentUser();
});
(From: https://developer.spotify.com/docs/apps/api/1.0/library_.html)
I suppose that my object returnedLibrary has all the playlists for the current user, isn't it? My problem is that I don't know what to do with this returned object and how to iterate through it.
Anyone knows how to access this information?
Thanks,
If you are interested in the user's playlists, you can do it like this:
var returnedLibrary;
require(['$api/library#Library'], function(Library) {
returnedLibrary = Library.forCurrentUser();
returnedLibrary.playlists.snapshot().done(function(snapshot) {
for (var i = 0, l = snapshot.length; i < l; i++) {
var playlist = snapshot.get(i);
// do something with playlist
}
});
});
Finally I have achieved my purpose:
var returnedLibrary;
require(['$api/library#Library'], function(Library) {
returnedLibrary = Library.forCurrentUser();
returnedLibrary.playlists.snapshot().done(function(snapshot) {
for (var i = 0, l = snapshot.length; i < l; i++) {
var playlist = snapshot.get(i);
aplay._collections();
aplay.tracks.snapshot().done(function(s){
selectedPlaylist = s;
for(k=0;k<s.length;k++){
s.get(k).name; //Will be the name of that song
}
});
}
});
});
Nevertheless, I don't know why I have to call aplay._collections(); (it seems is a private method). If I not do this call the tracks property is not set... Any ideas about this?
Thanks!

Categories

Resources