如何从Backbone中的对象获取元素?

时间:2017-02-03 21:29:46

标签: javascript backbone.js backbone-collections

我有对象模型:

var wo = new WordModel({
    "url": window.location.href,
    "time": toDay(),
    "w": w.trim()
});

timelineCollection.add(wo);

我尝试在timelineCollection time 04/02/2017 var o = { time: "04/02/2017" }; var filtered = timelineCollection.where(o); console.log(filtered); 中获取所有元素。我试过这个:

.andExpression("(startTime / 3600) - ((startTime / 3600) % 1")

但它对我不起作用

1 个答案:

答案 0 :(得分:1)

Backbone的集合where function实际上是您应该使用的。



// short syntax, every object becomes a Backbone.Model by default.
var collection = new Backbone.Collection([{
    id: 0,
    time: "04/02/2017",
  }, {
    id: 1,
    time: "05/02/2017",
  },
  // you can mix both plain objects and Model instances
  new Backbone.Model({
    id: 2,
    time: "04/07/2017",
  }), new Backbone.Model({
    id: 3,
    time: "04/02/2017",
  })
]);

// passing an existing model works too.
var model = new Backbone.Model({
  id: 4,
  time: "04/02/2017",
});

collection.add(model);

console.log(collection.where({
  time: "04/02/2017"
}));

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
&#13;
&#13;
&#13;