imtg
imtg

Reputation: 23

Nativescript - Update dynamic textfield source on click

I just want to ask how can I update the dynamic textfield upon a button click? The textfield is dynamic and the value should come from observable.

Code Behind

var observableModule = require("data/observable");
var source = new observableModule.Observable();

var HomePage = function() {};
HomePage.prototype = new BasePage();
HomePage.prototype.constructor = HomePage;

HomePage.prototype.contentLoaded = function(args) {
    var page = args.object;

    source.textSource = "sample";

    var layout = page.getViewById("stackID");
    var textField = new TextFieldModule.TextField();

    var textFieldBindingOptions = {
        sourceProperty: "textSource",
        targetProperty: "text",
        twoWay: false
    };

   textField.bind(textFieldBindingOptions, source);

   layout.addChild(textField);
}

HomePage.prototype.buttonTap = function() {
  source.textSource = "new word";
  source.update();
}

XML

<stack-layout loaded="contentLoaded" id="stackID">
    <Button tap="buttonTap" text="Update" />
</stack-layout>

Upvotes: 0

Views: 363

Answers (1)

imtg
imtg

Reputation: 23

I was able to find on how to update the source on click.

HomePage.prototype.onTap = function() {
  source.set("textSource", "new word");
}

Source: http://docs.nativescript.org/cookbook/data/observable

Upvotes: 2

Related Questions