www.808888web.comBackboneJScollection.reset()方法

意见反馈 ww9992019com 发表于 5 天前

www.808888web.com开户请拨18288069766红果果绿泡泡产女东莞塘厦镇htXx6U<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="7348">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

重置集合并用新模型填充数组或将清空整个集合。</p>

<h2>

语法</h2>

<pre courier new monospace padding:>

collection.reset(models,options)
</pre>

<h3>

参数:</h3>

<ul>

<li>

    models: 包含的集合实例的名称,这是需要被复位的集合。</li>

<li>

    options: 选项包括空值清空集合。</li>

</ul>

<h2>

示例</h2>

<pre class="prettyprint tryit prettyprinted" courier new monospace border-radius: width: line-height: font-size: overflow: auto color: rgb cursor: default background-image: url background-attachment: initial background-size: background-origin: background-clip: background-position: background-repeat: no-repeat>

<span class="dec">&lt;!DOCTYPE html&gt;</span><span class="pln">

</span><span class="tag">&lt;head&gt;</span><span class="pln">

  </span><span class="tag">&lt;title&gt;</span><span class="pln">Collection Example</span><span class="tag">&lt;/title&gt;</span><span class="pln">

     </span><span class="tag">&lt;script</span><span class="pln"> </span><span class="atn">src</span><span class="pun">=</span><span class="atv">"https://code.jquery.com/jquery-2.1.3.min.js"</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">&gt;&lt;/script&gt;</span><span class="pln">

     </span><span class="tag">&lt;script</span><span class="pln"> </span><span class="atn">src</span><span class="pun">=</span><span class="atv">"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">&gt;&lt;/script&gt;</span><span class="pln">

     </span><span class="tag">&lt;script</span><span class="pln"> </span><span class="atn">src</span><span class="pun">=</span><span class="atv">"https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">&gt;&lt;/script&gt;</span><span class="pln">

</span><span class="tag">&lt;/head&gt;</span><span class="pln">

</span><span class="tag">&lt;body&gt;</span><span class="pln">

  </span><span class="tag">&lt;script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">&gt;</span><span class="pln">

     </span><span class="com">//The 'C_Name' is a model name and includes default value</span><span class="pln">

     </span><span class="kwd">var</span><span class="pln"> C_Name </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Backbone</span><span class="pun">.</span><span class="typ">Model</span><span class="pun">.</span><span class="pln">extend</span><span class="pun">({</span><span class="pln">

        defaults</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

           country</span><span class="pun">:</span><span class="pln"> </span><span class="str">"sachin"</span><span class="pln">

        </span><span class="pun">}</span><span class="pln">

     </span><span class="pun">});</span><span class="pln">



     </span><span class="com">//'PlayersCollection' is an instance of collection and model 'C_Name' is specified by using model property</span><span class="pln">

     </span><span class="kwd">var</span><span class="pln"> </span><span class="typ">PlayersCollection</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Backbone</span><span class="pun">.</span><span class="typ">Collection</span><span class="pun">.</span><span class="pln">extend</span><span class="pun">({</span><span class="pln">

        model</span><span class="pun">:</span><span class="pln"> C_Name

     </span><span class="pun">});</span><span class="pln">



     </span><span class="com">//The 'country1' and 'country2' are the instances of the model 'C_name'</span><span class="pln">

     </span><span class="kwd">var</span><span class="pln"> country1 </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> C_Name</span><span class="pun">({</span><span class="pln">country</span><span class="pun">:</span><span class="pln"> </span><span class="str">"australia"</span><span class="pun">});</span><span class="pln">

     </span><span class="kwd">var</span><span class="pln"> country2 </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> C_Name</span><span class="pun">({</span><span class="pln">country</span><span class="pun">:</span><span class="pln"> </span><span class="str">"england"</span><span class="pun">});</span><span class="pln">



     </span><span class="com">//Add the model instances to the collection using 'mycollection' collection instance</span><span class="pln">

     </span><span class="kwd">var</span><span class="pln"> mycollection </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">PlayersCollection</span><span class="pun">();</span><span class="pln">

     mycollection</span><span class="pun">.</span><span class="pln">add</span><span class="pun">([</span><span class="pln">country1</span><span class="pun">,</span><span class="pln">country2</span><span class="pun">]);</span><span class="pln">



     </span><span class="com">//The 'length' property defines length of the collection</span><span class="pln">

     document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">'Number of added countries: '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> mycollection</span><span class="pun">.</span><span class="pln">length</span><span class="pun">);</span><span class="pln">

     document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">"&lt;br&gt;"</span><span class="pun">);</span><span class="pln">



     </span><span class="com">//Here, the reset() method resets the collection otherwise empties the collection</span><span class="pln">

     mycollection</span><span class="pun">.</span><span class="pln">reset</span><span class="pun">();</span><span class="pln">

     document</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">'Number of countries after reset: '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> mycollection</span><span class="pun">.</span><span class="pln">length</span><span class="pun">);</span><span class="pln">

  </span><span class="tag">&lt;/script&gt;</span><span class="pln">

</span><span class="tag">&lt;/body&gt;</span><span class="pln">

</span><span class="tag">&lt;/html&gt;</span><span class="pln">

</span></pre>

<h2>

输出</h2>

<p>

让我们进行以下步骤来看看上面的代码工作:</p>

<ul>

<li>

    <p>

        保存上述代码在文件reset.html</p>

</li>

<li>

    <p>

        在浏览器打开这个HTML文件。<br>

        <img alt="" src="/uploads/allimg/201505/1-15051G53402P2.png"></p>

</li>

</ul>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="BackboneJS collection.remove()方法"><i class="layui-icon"></i>
                            BackboneJS collection.remove()方法</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:BackboneJS collection.set()方法"><i class="layui-icon"></i>
                            BackboneJS collection.set()方法</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复