I'm trying to update a model from a switch in a tableViewRow controller using $model. I just want it to flip a value in the model when it flips back and forth but when I flip the switch it flips back false the first time I flip it and also doesn't seem to save the model so I'm obviously doing something wrong. Then when I continue to flip it back and forth it bounces around a bit.
I'm using Alloy 1.3 and Titanium SDK 3.2.2.
I have a tableview bound to a collection:
<Alloy> <Window id="win"> <View class="container-view"> <Label class="label-H5">Set a device to be visible or not and change order of devices.</Label> <TableView id="devicesTableView" class="tableview-form" dataCollection="device"> <Require src="settingsDeviceRow"/> </TableView> <Button id="closeBtn">Save and Close</Button> </View> </Window> </Alloy>Then I have a settingsDeviceRow view and controller:
<Alloy> <TableViewRow class="deviceTableViewRow"> <Label class="deviceRowLbl" text="{displayName}"></Label> <Label class="deviceRowTypeLbl" text="{type}"></Label> <Switch id="switch" class="deviceRowSwitch" titleOn="Visible" value="{showInView}"></Switch> </TableViewRow> </Alloy>
if ($model) { $.settingsDeviceRow.model = $model.toJSON(); } $.switch.addEventListener('change', function(e) { //if switch on update ShowInView to true. If switch off inListView = false if(e.value){ Ti.API.info("Switch set to True!"); //update the model to make showInView false. //$model.attributes.showInView = true; //which works? $model.get('showInView').value = true; }else{ Ti.API.info("Switch set to False!"); //$model.attributes.showInView = false; //which works? $model.get('showInView').value = false; } $model.save(); });I wouldn't mind just saving the entire collection either after flipping the switches if that's the best way to do it but I can't seem to get that to work either:
$.closeBtn.addEventListener('click', function () { //save all of the model elements in the collection Alloy.Collections.device.each(function(d) { d.save(); }); $.destroy(); $.win.close(); });