Created
November 27, 2015 11:03
-
-
Save jvandijk/49326ec22320e7b8d91b to your computer and use it in GitHub Desktop.
Multi list section with Backbone collections in Titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prepareItem(data) { | |
var item = { | |
properties: { | |
itemId: data.get('id') | |
}, | |
template: 'default', | |
title: { | |
text: "undefined" != typeof data.get("title") ? data.get("title") : '' | |
} | |
}; | |
if (OS_IOS) { | |
item.properties.selectionStyle = Ti.UI.iPhone.ListViewCellSelectionStyle.NONE; | |
} | |
return item; | |
} | |
function setup() { | |
var items = {}, | |
sections = [], | |
collection = Alloy.Collections.instance('your_model'), | |
viewTSS = $.createStyle({ | |
classes: ['sectionClass'] | |
}), | |
// always re-sort using the backbone comparator | |
collection.sort(); | |
_.each(collection.models, function(item, index) { | |
// divide data into sections | |
var unique = item.get('<section_id>').toString(); | |
if (!items[unique]) { | |
items[unique] = { | |
"allitems": [] | |
}; | |
} | |
items[unique].allitems.push(prepareItem(item)); | |
}); | |
_.each(items, function(elm, index) { | |
var view = Ti.UI.createView(), | |
label = Ti.UI.createLabel({ | |
text: index | |
}); | |
view.applyProperties(viewTSS); | |
view.add(label); | |
var section = Ti.UI.createListSection({headerView: view}); | |
section.setItems(elm.allitems); | |
sections.push(section); | |
}); | |
$.yourlistview.setSections(sections); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment