Nativescript:bindingContext到ListView

时间:2016-10-16 20:23:46

标签: android listview nativescript

我是Nativescript的新手并且正在尝试开发一个Android应用程序。我正在发送一个GET请求,并且我已经构建了一个Listview.Now,我正在尝试在选择Listview的项目时发送POST请求。但我无法绑定上下文。所以,我的POST请求没有正常工作。我的问题是如何绑定listview的上下文。

Js控制器:

var CategoryViewModel = require("../../shared/category_view_model");

var fetchModule = require("fetch");

var dialogsModule = require("ui/dialogs");

var observableModule = require("data/observable")

var page;

var data;

var categoryList = new CategoryViewModel([]);  
var pageData = new observableModule.fromObject({
    categoryList: categoryList
});



var UserViewModel = require("../../shared/brand_view_model");
var user = new UserViewModel();



exports.loaded = function(args) {
    page = args.object;

    page.bindingContext = pageData;

    categoryList.empty();
    categoryList.load();


};

function getBrand() {

 user.register();

}
exports.brand=function (args) {


    getBrand();
}

查看:

<Page loaded="loaded">
    <GridLayout>
        <ListView items="{{ categoryList }}" itemTap="brand">
            <ListView.itemTemplate>
                <Label text="{{ category }}" horizontalAlignment="left" verticalAlignment="center"  />
            </ListView.itemTemplate>
        </ListView>

    </GridLayout>
</Page>

POST请求的查看模型:

var fetchModule = require("fetch");
var Observable = require("data/observable").Observable;

 var observableArray = require("data/observable-array");
 var goodsList = new observableArray.ObservableArray([]);
 var pageData = require("data/observable").Observable;




function user(info) {
    info = info || {};

    // You can add properties to observables on creation
    var viewModel = new Observable({
        category: info.category || "",

    });

    viewModel.register = function() {



    fetch("http://10.0.2.2:8000/user_signup", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ 


    category:  viewModel.get("category"), 


    })
}).then(r => { return r.json(); }).then(function (r) {
     console.log("Success");
}, function (e) {
         console.log("Error occurred " + e);
    });


    };

         return viewModel;
}

    function handleErrors(response) {
    if (!response.ok) {
        console.log(JSON.stringify(response));
        throw Error(response.statusText);
    }
    return response;
}

module.exports = user;

1 个答案:

答案 0 :(得分:3)

你可以这样试试

JS控制器

exports.brand=function (args) {
    var list = args.object;
    var item = list.bindingContext; 
    user.register(item);
}

查看模型

viewModel.register = function(item) {
    //item points from user.register(item);
    fetch("http://10.0.2.2:8000/user_signup", {
       method: "POST",
       headers: { "Content-Type": "application/json" },
       body: JSON.stringify({ 
          category:  item, 
       })
       }).then(r => { return r.json(); }).then(function (r) {
              console.log("Success");
       }, function (e) {
            console.log("Error occurred " + e);
       });
};