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
最满意答案
要在倒数第二行更改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 withp
. Lastly, we swap pattern and hold space again withx
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.
相关问答
更多-
sed 's/^Done [0-9]*$//' infile sed 's/^Done [0-9]*$//' infile
-
为什么Processing.py会跳过我的数组的倒数第二项?(Why is Processing.py skipping the second-to-last item of my array?)[2022-02-27]
在每种情况下,你的第一个影响你的第二个。 对于UP ,如果selector为6,则变为7,然后匹配selector == 7 ; 对于DOWN ,如果selector为1,则它变为0,然后匹配selector == 0 。 使用elif使它们成为独家: if selector < 8: fill(fillcolors[selector]) selector += 1 elif selector == 7: fill(fillcolors[selector]) selector ... -
Grep倒数第二行(Grep penultimate line)[2022-07-06]
您可以像这样使用head和tail的组合,例如: $ cat input one two three HIDDEN four $ head -n -2 input ; tail -n 1 input one two three four 来自coreutils头文档: '-n k' '--lines = K' 输出前k行。 但是,如果k以' - '开头,则打印除每个文件的最后k行之外的所有行。 大小乘数后缀与-c选项相同。 因此, head -n -2部分除了其输入的最后两行之外的所有部分。 遗憾的是 ... -
在给定位置的情况下,您可以使用sed在特定行之前插入一行: sed '4i\My New Line\' my_file.txt 将在第四行插入“我的新行” 您可以使用wc -l来获取文件中的行数: $ wc -l < my_file.txt 5 完整示例直接回答您的问题: $ cat my_file.txt Hello World There is another line $ sed -i "`wc -l < my_file.txt`i\\My New Line\\" my_file.txt $ ...
-
要在倒数第二行更改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 col ...
-
以下内容应该可以工作(请注意,在某些系统中,您可能需要删除所有评论): sed '1 { # if this is the first line h # copy to hold space d # delete pattern space and return to start } /^}},$/ { # if this line matches regex /^}},$/ x # e ...
-
你不需要知道什么是old ; 只需重新定义整个行: import sys import fileinput for line in fileinput.input([filename], inplace=True): if line.strip().startswith('initial_mass = '): line = 'initial_mass = 123\n' sys.stdout.write(line) You don't need to know what ol ...
-
使用GNU sed: sed '0,/$node_(2)/{//!d;s//text to add\n&/}' file With GNU sed: sed '0,/$node_(2)/{//!d;s//text to add\n&/}' file
-
您可以使用此sed命令: sed -i '/another-pattern/s/pattern/new-string/g' ./file 这将仅在与another-pattern模式匹配的行上执行替换。 You can use this sed command: sed -i '/another-pattern/s/pattern/new-string/g' ./file This will perform substitution only on lines that match another- ...
-
使用GNU sed: sed '/^# specific comment line$/{n;s/.*/foo/}' file 输出: # specific comment line foo With GNU sed: sed '/^# specific comment line$/{n;s/.*/foo/}' file Output: # specific comment line foo