Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active December 18, 2015 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronksaunders/8b4ee4352bde73e4f15a to your computer and use it in GitHub Desktop.
Save aaronksaunders/8b4ee4352bde73e4f15a to your computer and use it in GitHub Desktop.
Alloy ListView & Facebook Friends
/**
* update the list view
*/
function createListViewInCode(_data) {
// create the template here
var myTemplate = {
childTemplates : [{// Image justified left
type : 'Ti.UI.ImageView', // Use an image view for the image
bindId : 'pic', // Maps to a custom pic property of the item data
properties : {// Sets the image view properties
width : '50dp',
height : '50dp',
left : 0
}
}, {
type : 'Ti.UI.Label',
bindId : 'textLabel',
properties : {
color : '#000',
left : '60dp',
top : 0,
textAlign : 'left'
}
}],
properties : {
height : '50dp',
//height : Ti.UI.SIZE
}
}
// assign the template to the ListView we created
$.list.templates = {
'template1' : myTemplate
};
var sections = [];
// create the section for the ListView
var dataSection = Ti.UI.createListSection({
headerTitle : 'Data'
});
// Add the section to the section array, we will have one element in the
// array and we MUST define it, unlike in the tableView
sections.push(dataSection);
// this is pretty straight forward, assigning the values to the specific
// properties in the template we defined above
var items = [];
for (var i in _data) {
items.push({
textLabel : {
text : _data[i].name
},
pic : {
image : _data[i].pic_square
}
});
}
dataSection.setItems(items);
// when all done, assign the section array to the ListView
$.list.sections = sections;
}
function _doFacebookLoginAction() {
if (!fb.loggedIn) { debugger;
fb.permissions = ["read_stream", "email"];
fb.authorize();
return;
}
// query your facebook friends using facebook query language
var query = "SELECT uid, name, pic_square, hometown_location FROM user ";
query += "where uid IN (SELECT uid2 FROM friend WHERE uid1 = " + fb.uid + ")";
query += "order by last_name limit 1000";
Ti.API.info("user id " + fb.uid);
fb.request("fql.query", {
query : query
}, function(r) {
if (r.success) {
createListViewInCode(JSON.parse(r.result));
} else { debugger;
}
});
// set login callback
fb.addEventListener('login', function(e) {
_doFacebookLoginAction();
});
}
$.index.open();
var fb = require("facebook");
// set this in your tiapp.xml file
fb.appid = Ti.App.Properties.getString("ti.facebook.appid");
_doFacebookLoginAction();
".container": {
backgroundColor:"white"
},
"Label": {
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000"
}
<Alloy>
<Window class="container">
<ListView id="list" defaultItemTemplate="template1">
</ListView>
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment