Conditional data binding in Alloy - appcelerator

Alloy supports data binding with collections and single models.
How can I conditionally style view elements based on models attribute value?
eg: if model.status = 'open' I want to give red background.

Taking ListView as an example.
Markup:
<ListSection dataCollection="books" dataTransform="doTransform">
<ListItem title:text="{title}" subtitle:text="{subtitle}" itemId="{id}" backgroundColor="{backgroundColor}" />
</ListSection>
Controller:
function doTransform(model){
var transform = model.toJSON();
transform.backgroundColor = transform.status=="open"?"red":"white";
return transform;
}
Docs

Related

why text changed is not reflected to viewmodel property

I have a property name
var viewModel = observableModule.fromObject({
name: "abcd",
onUsernameChanged: function () {
console.log("-->"+ viewModel.name)
}
});
textfield like below,
<TextField text="{{name}}" textChange="{{ onUsernameChanged }}"
class="h2 description-label" />
When I change the text by typing, It always prints --->abcd only.
Why the viewmodel property is not updated ?
Playground Demo of problem
currently I'm reading the changed text like below, And this method is not flexible and easy to use like viewmodel
var view = require("ui/core/view");
var idUsername = view.getViewById(args.object, "idUsername");
By taking a look at your code, I am inferring that there is no binding between your view (the XML file containing the TextField—assuming you are using NativeScript Core) and your view-model (the javascript file where you have your observable viewModel).
When the textChange event emits, you are printing the stored value at your ViewModel over and over (name, in this case: abcd). For example, at first, your value is abcd, but the text might be empty. Later, you type A, and then it prints abcd again, and so on.
If you want to print the new text output TextField element, you have to use the output emitted by the event. Look at this excerpt:
const viewModel = observableModule.fromObject({
name: "abcd",
onUsernameChanged: function (eventData) {
// eventData has information on the textChange event
console.dir(eventData);
// Do whatever you want with this data, such as updating the view model
}
});
try using this.get()
console.log("-->"+ this.get('name'))

Getting Kendo Grid's ID Field WithJavascript

I have a function that I'm going to use with several different Grids. In this function I want to set Grid's Id field which varies in different Grids.
How can I get each grid's ID?
function incId(e) {
var model = e.model;
var grid = e.sender;
//removed
model.set(!IDFIELD!, ++Id);
}
You can use model.idField to get it
model.set(model.idField, ++Id);
Please read its documentation for further investigation

How can I change ItemView to CompositeView in Marionette application?

I'm trying to make in-row editing in Marionette application. What is the best approach to do this?
I have table which is Marionette.CompositeView and rows of this table are Marionettes ItemViews. Now I'm trying to change clicked table row (ItemView) to a CompositeView which will contain inputs and selects with ajax fetched data. Is this a good approach?
You can use CollectionView.getChildView to render different view for edited items, but this can cause performance problems if you need to render large collections.
I modified Derick Bailey's Tree View example to show how this can be done - http://jsfiddle.net/msamujlo/8g3abfg2/
// The recursive tree view
var TreeView = Backbone.Marionette.CompositeView.extend({
template: "#node-template",
tagName: "ul",
getChildView: function(item){
return item.get('isEditable')? EditorView : TreeView;
},
// ... more methods
};
var EditorView = TreeView.extend({
template: "#editor-template",
});
...

Can the content in Alloy Controller.getView() can be updated?

I am using Alloy 1.3. Can the content in the Controller.getView() can be updated? For example:
In Alloy, if we have view view.xml
<Alloy>
<View>
<Label id="label1"/>
... other content ...
</View>
</Alloy>
In view.js
exports.updateLabel = function(value){
$.label1.text = value;
}
If I have another controller e.g. index.js
var v = Alloy.createController('view').getView();
// assume $.win is the <Window> in index.xml
$.win.add(v);
function updateContent(value){
// This is not work. I want to know how it can be updated
// after the controller turned into a view
v.updateLabel(value);
}
Updating content on object returned from controller.getView() method is fine. In your view.js example you can change label1 text in two different ways:
exports.updateLabel = function(value){
$.label1.text = value;
}
or
exports.updateLabel = function(value){
$.getView('label1').text = value;
}
If you are calling $.getView() without any parameters it will return top level view which has the same id as name of your controller and view.

Define a dataObject and pass it to the next view in Flashbuilder

I am trying to convert my java-app for Android to a Flex/Air app with Flashbuilder.
I am nearly there (thanks to sample code on Adobe) but have problems with passing data between views.
I have 3 views. The first have a list of items and an event handler that choose one of this list items and pass this to the next view:
<s:List id="list" left="0" top="0" bottom="0" width="768"
change="navigator.pushView(Intro, list.selectedItem)" dataProvide "{data}">
This works fine and I can use the values stored in {data}.
e.g.
<s:Label text="{data.title}"/>
Now I want to pass the same data on a button click to the next view, spelaView.
Something like this:
<s:Button id="backBtn" label="Spela"
click="navigator.pushView(SpelaSaga, dataObj)" />
Sorry to say, I do not know how I transform the data-object {data} (with three items: data.title, data.description, data.audio) to dataObj in a form that the next view are abel to use.
Hope someone is kind enough to give me some help on this.
I just solved it.
Defined this function and could then get the data in the next view
protected function spelaSaga():void{
var dataObj:Object=
{
titel:data.title bild:data.description, audio:data.audio
};
navigator.pushView(views.SpelaSaga, dataObj);
}

Resources