Javascript 读取StackMob中的所有对象值
我一直在使用StackMob的fetch()函数来检索表中的所有对象,fetch()方法将其作为单个Javascript 读取StackMob中的所有对象值,javascript,json,stackmob,Javascript,Json,Stackmob,我一直在使用StackMob的fetch()函数来检索表中的所有对象,fetch()方法将其作为单个模型对象给出。每个对象都有多个具有相应值的属性。然后,我使用了JSON.stringify(model)来获得以下输出: {"0":{"attribute1":val1,"attribute2":"val2","attribute3":val3,"attribute4":"val4"}, "1":{"attribute1":val1,"attribute2":"val2","attribute3"
模型对象给出。每个对象都有多个具有相应值的属性。然后,我使用了JSON.stringify(model)
来获得以下输出:
{"0":{"attribute1":val1,"attribute2":"val2","attribute3":val3,"attribute4":"val4"},
"1":{"attribute1":val1,"attribute2":"val2","attribute3":val3,"attribute4":"val4"},
"2":{"attribute1":val1,"attribute2":"val2","attribute3":val3,"attribute4":"val4"}
……等等
如何仅打印出这些值中的每一个
StackMob有一个get()函数,可以这样使用:
var a_book = new Book({ 'title': 'The Complete Calvin and Hobbes', 'author': 'Bill Watterson' });
console.debug(a_book.get('title')); //prints out "The Complete Calvin and Hobbes"
但我不确定在我检索表中的所有对象而不是创建新对象(如上所述)的情况下如何使用它。由于stackmob js sdk构建在backbonejs上,您可以声明一个集合,其中可以包含“表”中的所有“行”然后遍历集合,对“表”中的每个项执行您想要的任何操作
这里是StackMob的雇员。正如blackmwana所指出的,我们构建在主干之上,因此您可以使用上面提到的主干集合:{async:true}
块不是必需的,因为StackMob执行异步调用。此外,每个调用都应该在fetch回调的success
块内完成.fetch({success:function(results){results.each…}})
。布莱克姆瓦纳几乎回答了这个问题:)。谢谢布莱克姆瓦纳
var MyModel = StackMob.Model.extend({
schemaName:'YOUR_SCHEMA_NAME'
});
var MyCollection= StackMob.Collection.extend({
model:MyModel
});
var myCollection = new MyCollection();
myCollection.fetch({async: true});
myCollection.each(function(item){
//do something with item, maybe print
});