javascript-Telerik Nativescript移动应用程序中的基本模糊事件
作者:互联网
我正在使用带有NativeScript的Telerik AppBuilder编写跨平台的移动应用程序.我正在努力弄清楚如何在TextField上获取基本的“模糊”或“ onTextChanged”事件.我可以弄清楚如何执行“轻击”事件之类的事情,但是为什么很难找到某人进行“模糊”或“ onTextChanged”的样本呢?
我已经试过了:
var tagField = view.getViewById(page, "tfDescription")
tagField.set("onblur", function(eventData) {
pageData.set("debug", "blur this field: " + JSON.stringify(eventData));
});
我还尝试了xml中可以想到的每个属性:
<TextField id="tfDescription" blur="blur" onblur="blur" onLoseFocus="blur" focusLost="blur" textChanged="blur" row="2" text="{{ description }}" />
这些都不起作用.有人知道如何执行此操作吗?
谢谢
解决方法:
据我所知,NativeScript中没有模糊(类似)事件.但是,您可以对更改文本时做出反应.
您要做的是利用Data Binding Mechanisms和
NativeScript中的Observable Object.
简要地说,数据绑定将使您能够将用户界面(最经常在XML文件中描述)与业务模型/数据对象连接.数据绑定可以是“单向”的,这意味着您对数据对象所做的任何更改都将反映在UI中.也可以有两种方式,这意味着您在UI中所做的任何更改也将反映在您的数据对象中.
对于您的情况,您希望双向绑定,因为您希望UI(用户输入文本)中发生的任何事情都反映在模型中.
例
XML
假设您有以下xml:
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<StackLayout>
<TextField text="{{ description }}" />
</StackLayout>
</Page>
JS
我已在内联评论以方便理解.
var observable = require("data/observable");
function pageLoaded(args) {
var page = args.object;
/**
* Creating a new Observable object.
*/
var contextObj = new observable.Observable();
/**
* Setting the property 'description' to 'hi there'.
* In your XML you'll attach this property to the
* Text field as such:
* <TextField text="{{ description }}" />
* This means that the Text field will, by default
* be prepopulated with 'Hi there'
*/
contextObj.set("description", "Hi there");
/**
* Attaching the observable object to the
* 'bindingContext' of the page. This is a
* special property to which you want to attach
* the object. By default bindings are two way.
*/
page.bindingContext = contextObj;
/**
* Event listener.
* When the user is changing the text in the UI, this gets propagated down
* to the contextObj which will change. Here we add an event listener for
* changes and log some information about the change that just happened.
*/
contextObj.addEventListener(observable.Observable.propertyChangeEvent, function (event) {
console.log('The object changed!');
console.log('Event: ' + event.eventName);
console.log('propertyName: ' + event.propertyName);
console.log('New value: ' + event.value);
console.log('');
});
}
exports.pageLoaded = pageLoaded;
标签:nativescript,javascript,mobile,telerik 来源: https://codeday.me/bug/20191012/1902877.html