首页 \ 问答 \ Ajax异步问题(Ajax asynchronous issue)

Ajax异步问题(Ajax asynchronous issue)

我正在使用backbone.js构建一个移动应用程序。 它允许用户选择团队中的足球运动员。 我希望应用程序能够记住,如果用户中途休息,选择了哪些玩家。 我这样做与本地存储。 所以说用户过早退出后返回“选择玩家”页面,已经选择了2个玩家。 我为这个页面生成一个html字符串:

html_string = template();

这是一系列div和嵌套div。 然后我从本地存储中获取saved_selection:

saved_selection = JSON.parse(window.localStorage.getItem('match_selection'));
console.log(saved_selected);

输出是:

Object {236: "Forward_2", 243: "Forward_1"}

关键是玩家ID,值是他们的位置。 接下来,我遍历每个对象键值对并替换匹配的div(在此示例中我不提供)。 所以完整的渲染功能是:

    render: function (forArr) {
        saved_selection = JSON.parse(window.localStorage.getItem('match_selection'));

        html_string = template({forArr:forArr, saved_selection:saved_selection});

        console.log(saved_selection);

        if(UsefulFuncs.getObjSize(saved_selection)>0){
                for(var index in saved_selection) {  
                    var player = new models.Player({id: index});
                    player.fetch({success:function(model) {
                console.log('in success');  
            //do some matching and div replacement
                            html_string = "The new html string";
                       }});

                }
        }
        this.$el.html(html_string);
        return this;
    },

我可以在输出中看到它进入成功两次,但返回的html仍然是原始模板,而不是“新的html字符串”....我认为这可能与异步调用有关... 。


Im building a mobile app with backbone.js. It allows users to choose football players on a team. I want the app to remember what players have been choosen if the users stop half way through. Im doing this with local storage. So say the user returns to the "choose players" page after exiting prematurely, having already choosen 2 players. I generate a html string for this page with:

html_string = template();

This is a series of divs and nested divs. I then get the saved_selection from local storage:

saved_selection = JSON.parse(window.localStorage.getItem('match_selection'));
console.log(saved_selected);

The output is:

Object {236: "Forward_2", 243: "Forward_1"}

The key is the player id and the value is their position. Next, I loop through each object key-value pair and replace matched divs (i dont provide that in this example). So the full render function is:

    render: function (forArr) {
        saved_selection = JSON.parse(window.localStorage.getItem('match_selection'));

        html_string = template({forArr:forArr, saved_selection:saved_selection});

        console.log(saved_selection);

        if(UsefulFuncs.getObjSize(saved_selection)>0){
                for(var index in saved_selection) {  
                    var player = new models.Player({id: index});
                    player.fetch({success:function(model) {
                console.log('in success');  
            //do some matching and div replacement
                            html_string = "The new html string";
                       }});

                }
        }
        this.$el.html(html_string);
        return this;
    },

I can see in the output that it goes into the success twice, but the returned html is still the original template, and not "The new html string"....I think this may be something to do with asynchronous calls....


原文:https://stackoverflow.com/questions/18878466
更新时间:2023-10-19 21:10

最满意答案

要在倒数第二行更改Sweet to Green,但只有该行包含Sweet pepper

$ sed 'x; ${/Sweet pepper/s/Sweet/Green/;p;x}; 1d' file.txt
cabbage
spinach
collard greens
corn salad
Green pepper
kale

要将整个倒数第二行替换为胡萝卜,不管它包含什么,

$ sed 'x; ${s/.*/carrots/;p;x}; 1d' file.txt
cabbage
spinach
collard greens
corn salad
carrots
kale

怎么运行的

让我们采取这个命令并一次一步地检查它:

sed 'x; ${s/.*/carrots/;p;x}; 1d'
  • x

    这交换了模式空间(保存最近读取的行)和保持空间。

    完成后,保持空间将包含最近读取的行,并且模式空间将包含上一行。 (当我们刚读完第一行时就是例外,在这种情况下,保留空间会有第一行,模式空间将是空的。)

  • ${s/.*/carrots/;p;x}

    当我们在最后一行,由$表示时,模式空间保持倒数第二行,我们可以执行任何我们喜欢的替换或其他命令。 当我们完成后,我们用p打印倒数第二行。 最后,我们交换模式并用x再次保留空间,以使模式空间再次包含最后一行。 sed将打印这个,因为默认情况下,在命令结束时, sed打印模式空间中的任何内容。

  • 1d

    当我们在第一行时,用1表示,模式空间是空的(因为没有前一行),我们删除它( d )。

一个更简单的方法

这种方法很容易理解,代价是执行速度较慢:

$ tac file.txt | sed '2 {/Sweet pepper/s/Sweet/Green/}' | tac
cabbage
spinach
collard greens
corn salad
Green pepper
kale

而且,对于胡萝卜来说:

$ tac file.txt | sed '2 s/.*/carrots/' | tac
cabbage
spinach
collard greens
corn salad
carrots
kale

工作原理:在这里,我们使用tac来改变行的顺序。 注意:

$ tac file.txt
kale
Sweet pepper
corn salad
collard greens
spinach
cabbage

这样,倒数第二行成为第二行。因此,我们只是简单地告诉sed在第二行上操作。之后,我们再次使用tac来按照正确的顺序放置行。


To change Sweet to Green on the second to last line but only if that line contains Sweet pepper:

$ sed 'x; ${/Sweet pepper/s/Sweet/Green/;p;x}; 1d' file.txt
cabbage
spinach
collard greens
corn salad
Green pepper
kale

To replace the whole of the second to last line, regardless of what it contains, to carrots:

$ sed 'x; ${s/.*/carrots/;p;x}; 1d' file.txt
cabbage
spinach
collard greens
corn salad
carrots
kale

How it works

Let's take this command and examine it one step at a time:

sed 'x; ${s/.*/carrots/;p;x}; 1d'
  • x

    This exchanges the pattern space (which holds the most recently read line) and the hold space.

    When this is done, the hold space will contain the most recently read line and the pattern space will contain the previous line. (The exception is when we have just read the first line. In that case, the hold space will have the first line and the pattern space will be empty.)

  • ${s/.*/carrots/;p;x}

    When we are on the last line, indicated by the $, the pattern space holds the second to last line and we can perform whatever substitutions or other commands that we like. When we are done, we print the second to last line with p. Lastly, we swap pattern and hold space again with x so that the pattern space will again contain the last line. sed will print this because, by default, at the end of the commands, sed prints whatever is in the pattern space.

  • 1d

    When we are on the first line, indicated by the 1, the patten space is empty (because there was no previous line) and we delete it (d).

A still simpler method

This method is easy to understand at the cost of slower execution speed:

$ tac file.txt | sed '2 {/Sweet pepper/s/Sweet/Green/}' | tac
cabbage
spinach
collard greens
corn salad
Green pepper
kale

And, for carrots:

$ tac file.txt | sed '2 s/.*/carrots/' | tac
cabbage
spinach
collard greens
corn salad
carrots
kale

How it works: Here, we use tac to reverse the order of the lines. Observe:

$ tac file.txt
kale
Sweet pepper
corn salad
collard greens
spinach
cabbage

In this way, the second-to-last line becomes line number 2. Thus, we just simply tell sed to operate on line number 2. Afterward, we use tac again to put the lines but in correct order.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。