Nativescript 从外部所需模块/插件中的JSON响应更新视图模型

Nativescript 从外部所需模块/插件中的JSON响应更新视图模型,nativescript,Nativescript,如果我有如下代码(来自) 如果我想使fetch部分可重用,并使用require方法在外部文件中调用它。如何从这段代码中传递json响应: <!-- begin snippet: js hide: false console: true babel: false --> //this is fetch.js var response = ''; exports.fetch = function() { fetch("http://httpbin.org/ip", {

如果我有如下代码(来自)

如果我想使fetch部分可重用,并使用require方法在外部文件中调用它。如何从这段代码中传递json响应:

<!-- begin snippet: js hide: false console: true babel: false -->
//this is fetch.js
   var response = '';
   exports.fetch = function() {
   fetch("http://httpbin.org/ip", {
        method: "GET",
        headers: { "Content-Type": "application/json" }
    })
    .then(function (res) { return res.json(); })
    .then(function (data) {  
        console.log(data.origin); // make sure you are getting the value
        response = data;
    })};
<!-- language: lang-js -->

//这是fetch.js
var响应=“”;
exports.fetch=函数(){
取回(“http://httpbin.org/ip", {
方法:“获取”,
标题:{“内容类型”:“应用程序/json”}
})
.then(函数(res){return res.json();})
.then(函数(数据){
console.log(data.origin);//确保您正在获取该值
响应=数据;
})};
回到这里

<!-- begin snippet: js hide: false console: true babel: false -->
var observableModule = require("data/observable");
var fetchIt = require("./fetch.js");
var viewModel = new observableModule.Observable();
viewModel.set("ip", "none"); // initial value

function onLoaded(args) {
   var page = args.object;
   page.bindingContext = vm;
   var data = fetchIt.fetch;
   viewModel.set("ip", data);
}
exports.onLoaded = onLoaded;
<!-- language: lang-js -->

var observableModule=要求(“数据/可观察”);
var fetchIt=require(“./fetch.js”);
var viewModel=新的observableModule.Observable();
viewModel.set(“ip”、“无”);//初始值
已加载函数(args){
var page=args.object;
page.bindingContext=vm;
var data=fetchIt.fetch;
viewModel.set(“ip”,数据);
}
exports.onload=已加载;
我知道这段代码不会运行,所以仅将其用作我想要实现的示例


提前感谢。

如果您返回获取承诺,您可以执行一个。然后在它上在其他js文件中分配变量。我试图坚持使用typescript,但我相信下面的更改会对您有所帮助

<!-- begin snippet: js hide: false console: true babel: false -->
//this is fetch.js

   exports.fetch = function() {
   return fetch("http://httpbin.org/ip", {
        method: "GET",
        headers: { "Content-Type": "application/json" }
    })
    .then(function (res) { return res.json(); })
    .then(function (data) {  
        console.log(data.origin); // make sure you are getting the value
        return data;
    })};
<!-- language: lang-js -->

<!-- begin snippet: js hide: false console: true babel: false -->
var observableModule = require("data/observable");
var fetchIt = require("./fetch.js");
var viewModel = new observableModule.Observable();
viewModel.set("ip", "none"); // initial value

function onLoaded(args) {
   var page = args.object;
   page.bindingContext = vm;
   var data = fetchIt.fetch.then(function (data) {
       viewModel.set("ip", data);
    }
}
exports.onLoaded = onLoaded;
<!-- language: lang-js -->

//这是fetch.js
exports.fetch=函数(){
返回取回(“http://httpbin.org/ip", {
方法:“获取”,
标题:{“内容类型”:“应用程序/json”}
})
.then(函数(res){return res.json();})
.then(函数(数据){
console.log(data.origin);//确保您正在获取该值
返回数据;
})};
var observableModule=要求(“数据/可观察”);
var fetchIt=require(“./fetch.js”);
var viewModel=新的observableModule.Observable();
viewModel.set(“ip”、“无”);//初始值
已加载函数(args){
var page=args.object;
page.bindingContext=vm;
var data=fetchIt.fetch.then(函数(数据){
viewModel.set(“ip”,数据);
}
}
exports.onload=已加载;

如果您返回fetch承诺,您可以执行一个。然后在它上分配另一个js文件中的变量。我尝试使用typescript,但我相信以下更改将对您有所帮助

<!-- begin snippet: js hide: false console: true babel: false -->
//this is fetch.js

   exports.fetch = function() {
   return fetch("http://httpbin.org/ip", {
        method: "GET",
        headers: { "Content-Type": "application/json" }
    })
    .then(function (res) { return res.json(); })
    .then(function (data) {  
        console.log(data.origin); // make sure you are getting the value
        return data;
    })};
<!-- language: lang-js -->

<!-- begin snippet: js hide: false console: true babel: false -->
var observableModule = require("data/observable");
var fetchIt = require("./fetch.js");
var viewModel = new observableModule.Observable();
viewModel.set("ip", "none"); // initial value

function onLoaded(args) {
   var page = args.object;
   page.bindingContext = vm;
   var data = fetchIt.fetch.then(function (data) {
       viewModel.set("ip", data);
    }
}
exports.onLoaded = onLoaded;
<!-- language: lang-js -->

//这是fetch.js
exports.fetch=函数(){
返回取回(“http://httpbin.org/ip", {
方法:“获取”,
标题:{“内容类型”:“应用程序/json”}
})
.then(函数(res){return res.json();})
.then(函数(数据){
console.log(data.origin);//确保您正在获取该值
返回数据;
})};
var observableModule=要求(“数据/可观察”);
var fetchIt=require(“./fetch.js”);
var viewModel=新的observableModule.Observable();
viewModel.set(“ip”,“无”);//初始值
已加载函数(args){
var page=args.object;
page.bindingContext=vm;
var data=fetchIt.fetch.then(函数(数据){
viewModel.set(“ip”,数据);
}
}
exports.onload=已加载;

太棒了!非常感谢。我刚开始为我的高级项目学习这些东西,你的帮助非常宝贵。如果你愿意跟我一起学习,我很想在其他问题上多动脑筋。谢谢!太棒了!非常感谢。我刚开始为我的高级项目学习这些东西,你的帮助非常宝贵。我很乐意选择你如果你愿意跟我来的话,我会告诉你更多其他的问题。谢谢!