RxJS流的异步处理

时间:2017-01-16 03:52:55

标签: javascript asynchronous rxjs reactive-programming

//app.js
import $ from 'jquery';
import Rx from 'rxjs/Rx';

var $pull = $('#fetch');//this is a button id
var pull$ =Rx.Observable.fromEvent($pull,'click').startWith("/hcdata.json");

var $result = $(".datab");//class for HTML table
var classarray = ['id','type','name','image','tn'];

var resp$ = pull$.flatMap(
    function(reqdata){
        return Rx.Observable.fromPromise($.getJSON(reqdata));
    });

var pri$  = resp$.subscribe(
    function(data){
        var head = Object.keys(data[0]);
        var len = head.length;
        $result.empty();
        $("<tr>").appendTo($result);
        $.each(head,function(i,value){
        $("<th class='tablehead'>"+value.toUpperCase()+"`</th>").appendTo($result)})`
        $("</tr>").appendTo($result);
        console.log(head);

        for( var j= 0;j<data.length;j++)
        {
             $("<tr >").appendTo($result);
            for(var i =0;i<len;i++)
            {
                //alert(data[j][head[i]]);
                $("<td contenteditable='true' class='"+classarray[i]+"''>"+data[j][head[i]]+"</td>").appendTo($result)
            }
            $("</tr>").appendTo($result);
        }

    },
    function(err){
        console.log(err);
    },
    function(){
        console.log("Completed");
    });

    //element assignment
    var $idv = $(".id");
    var $typv = $(".type");
    var $nmv = $(".name");
    var $imgv = $(".image");
    var $tnv = $(".tn");

  //not working with no errors
    var abc$ = Rx.Observable.defer(function(){
        return Rx.Observable.fromEvent($idv,'blur');
    });  

    var lat$ = abc$.map(function(e){
        console.log(e.target.innerText);    
        return e.target.innerText;
    }).flatMap(function(data){
         return Rx.Observable.fromPromise(function(data){
            return Promise.resolve(data);
         });
    });

    var sub = lat$.subscribe(function(x){
        if (x === parseInt(x, 10))
            alert("data is integer")
        else
            alert("data is not an integer")
    },
    function(err){
        console.log(err);
    },
    function(){
        console.log("Finally");
    })

我从提到的JSON文件中获取数据并将其构建到一个表中,这很好。问题是我想尝试在编辑任何单元格时使用onblur事件,并进一步对所做的更改进行验证,此事件处理没有错误。我是这种异步反应式编程风格的新手。请帮忙。

1 个答案:

答案 0 :(得分:0)

它又是BackTick问题。修复此问题,然后您将能够理解实际错误。

//app.js
import $ from 'jquery';
import Rx from 'rxjs/Rx';

var $pull = $('#fetch');//this is a button id
var pull$ =Rx.Observable.fromEvent($pull,'click').startWith("/hcdata.json");

var $result = $(".datab");//class for HTML table
var classarray = ['id','type','name','image','tn'];

var resp$ = pull$.flatMap(
    function(reqdata){
        return Rx.Observable.fromPromise($.getJSON(reqdata));
    });

var pri$  = resp$.subscribe(
    function(data){
        var head = Object.keys(data[0]);
        var len = head.length;
        $result.empty();
        $("<tr>").appendTo($result);
        $.each(head,function(i,value){
        $("<th class='tablehead'>"+value.toUpperCase()+"`</th>").appendTo($result)})
        $("</tr>").appendTo($result);
        console.log(head);

        for( var j= 0;j<data.length;j++)
        {
             $("<tr >").appendTo($result);
            for(var i =0;i<len;i++)
            {
                //alert(data[j][head[i]]);
                $("<td contenteditable='true' class='"+classarray[i]+"''>"+data[j][head[i]]+"</td>").appendTo($result)
            }
            $("</tr>").appendTo($result);
        }

    },
    function(err){
        console.log(err);
    },
    function(){
        console.log("Completed");
    });

    //element assignment
    var $idv = $(".id");
    var $typv = $(".type");
    var $nmv = $(".name");
    var $imgv = $(".image");
    var $tnv = $(".tn");

  //not working with no errors
    var abc$ = Rx.Observable.defer(function(){
        return Rx.Observable.fromEvent($idv,'blur');
    });  

    var lat$ = abc$.map(function(e){
        console.log(e.target.innerText);    
        return e.target.innerText;
    }).flatMap(function(data){
         return Rx.Observable.fromPromise(function(data){
            return Promise.resolve(data);
         });
    });

    var sub = lat$.subscribe(function(x){
        if (x === parseInt(x, 10))
            alert("data is integer")
        else
            alert("data is not an integer")
    },
    function(err){
        console.log(err);
    },
    function(){
        console.log("Finally");
    })