2011-06-18 61 views
1

我有一个backbone.js和model.save()方法的问题。尽管没有需要三个库,但我已经在下面列出了一个完整的示例。Backbone.js问题当保存模型

我有一个标记模型,一个标记集合模型和一个模型来表示我的UI中的一些选定项目,称为搜索条件。

我在操作搜索标准的标签集合后尝试保存模型时出现url错误。这个例子说明了这个问题。

看来在本例中最后一次保存的调用中,主干无法解析标记集合模型中定义的url。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Test</title> 
    <script src="Scripts/Libraries/jquery-1.6.1.js" type="text/javascript"></script> 
    <script src="Scripts/Libraries/underscore.js" type="text/javascript"></script> 
    <script src="Scripts/Libraries/backbone.js" type="text/javascript"></script> 

    <script language="javascript" type="text/javascript"> 

     $(function() { 

      // Simple model for a tag. Tags have an id and a title. 
      var TagModel = Backbone.Model.extend({}); 

      // Collection for tags. 
      var TagCollection = Backbone.Collection.extend({ 
       model: TagModel, 
       url: "tags" 
      }); 

      // Sample model to hold a set of "selected" search criteria. 
      // Includes search text and a collection of "selected" tags. 
      var SearchCriteriaModel = Backbone.Model.extend({ 
       defaults: { 
        "searchText": "", 
        "tags": new TagCollection() 
       } 
      }); 

      // Create master tags collection. 
      window.tags = new TagCollection(); 
      window.tags.refresh([ 
       { id: 1, title: "Tag A" }, 
       { id: 2, title: "Tag B" }, 
       { id: 3, title: "Tag C" } 
      ]); 

      // Create search criteria. 
      window.searchCriteria = new SearchCriteriaModel(); 

      // Should be 3 tags. 
      console.log("Should be 3 tags in master tags list."); 
      console.log("Count = " + window.tags.size()); 

      // Should be 0 tags in criteria collection. 
      console.log("Should be 0 selected tags."); 
      console.log("Count = " + window.searchCriteria.get("tags").size()); 

      // Update tag title for tag 1. 
      var tag = window.tags.get(1); 
      tag.set({ "title": "Tag AA" }); 

      // Save tag. 
      console.log("Try to save tag. Should attempt PUT to /tags/1. WORKS.") 
      tag.save(); 

      // Add tag to search criteria. 
      window.searchCriteria.get("tags").add(tag); 

      // Should be 1 tag in criteria collection now. 
      // I am not moving the tag, but rather wanting to simply add a reference to the tag to the 
      // criteria collection. 
      console.log("Should be 1 selected tags."); 
      console.log("Count = " + window.searchCriteria.get("tags").size()); 

      // Should STILL be 3 tags in the master list. 
      console.log("Should be 3 tags in master tags list."); 
      console.log("Count = " + window.tags.size()); 

      // Update tag title for tag 1 again. 
      var tag = window.tags.get(1); 
      tag.set({ "title": "Tag AAA" }); 

      // Save tag. 
      console.log("Try to save tag. Should attempt PUT to /tags/1. WORKS.") 
      tag.save(); 

      // Remove tag from criteria. Simulates someone "deselecting" a tag from the search. 
      window.searchCriteria.get("tags").remove(tag); 

      // Should be 0 tags selected. 
      console.log("Should be 0 selected tags."); 
      console.log("Count = " + window.searchCriteria.get("tags").size()); 

      // Save tag. FAILS. 
      console.log("Try to save tag. Should attempt PUT to /tags/1, but does not. Instead throws error 'A url property or function must be specified'."); 
      tag.save(); 

     }); 

    </script> 

</head> 
<body> 
    <h1>Test</h1> 
    <p>Backbone test page.</p> 
</body> 
</html> 

有什么想法?谢谢!

修订

我更新的代码,以帮助说明,我不动集合的标签,而是增加了标签的参考第二集合。然后,当我从第二个集合(而不是第一个集合)中移除标记时,Backbone无法解析第一个集合,然后无法获取用于保存的URL。

我很困惑,为什么从一个集合中删除标签会影响到单独集合中对该标签的引用。

我来自C#背景。可能对象和集合在这里工作不同。

回答

1

你已经有了运行(一个窗口,一个在你的SearchCriteriaModel定义)标签两个集合,你似乎是移动两个集合

当您执行这行代码之间的标签:

window.searchCriteria.get("tags").remove(tag); 

标记和集合之间的链接已丢失。URL是从集合中确定的。

请注意,如果您删除上面的代码行,您的代码就会保存,因为它会从window.tags集合中获取URL。

Ps ..很好的问题,问得好,格式良好的代码示例演示了这个问题。

+0

感谢。我不认为我在“移动”标签。主标签集合是我主要的标签列表。搜索标准标签集合用于存储“选定”标签。我想要在两个集合中都有对该标签的引用。主集合用于显示我的标签列表,另一个集合用于跟踪选定的标签,以便我可以用它做其他事情,例如将条件发送到查找功能。我不确定为什么从标准集合中删除标签时集合引用会丢失。标签仍然是主要收藏的一部分。 – Kevin

+0

查看http://documentcloud.github.com/backbone/docs/backbone.html上注释的backbone.js源代码,您会看到当您执行collection.remove(model)时,清除了到集合的链接( model.collection)在collection._remove中。我坚持我的回答。一个模型只有一个内置的可导航链接到一个集合。该链接是模型可以用来确定URL的两种方式之一。 –

+0

我不是故意问你的答案,只是更好地理解它。很抱歉,如果这样的话。我现在明白了。我想我需要找出一种处理“搜索标准”标签集合的不同方法。标签将多次添加并从此集合中删除,但需要保留在主集合中(使用集合引用)。如果从一个集合中删除标签会破坏此引用,即使该模型仍然是其他的一部分收集,我会让汤姆想出不同的东西。再次感谢! – Kevin

0

不知道是否能解决你的问题,但你应该首先改变你的网址为“/标签”,而不仅仅是“标签”

+0

谢谢,但这不是问题。我将重点放在与Backbone相关的问题上,这些问题甚至可以在最终的示例调用保存中解析一个url。 – Kevin