0

I've been using StackMob's fetch() function to retrieve all objects in my table, given by the fetch() method as a single model object. Each object has multiple attributes with corresponding values. I've then used JSON.stringify(model) to get this output:

{"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"}

...and so on.

How can I print out just each of those values?

StackMob has a get() function that can be used as such:

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"

But I'm not sure how I would use it in my situation, where I'm retrieving all of the objects in my table, rather than creating a new object (as above).

Rob
  • 4,927
  • 12
  • 49
  • 54
user2181948
  • 1,646
  • 3
  • 33
  • 60

1 Answers1

0

since the stackmob js sdk is built on backbonejs you could declare a collection which can contain all the "rows" in your "table" and then iterate through the collection to perform whatever action you want to each item in your "table"

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
               });
  • StackMob employee here. As blackmwana noted, we're built on top of Backbone, so you can use Backbone's collections as mentioned above: http://backbonejs.org/#Collection-Underscore-Methods The `{async: true}` block isn't necessary since StackMob does async calls. Also, the `each` call should be done inside the `success` block of the fetch callback. `.fetch({ success: function(results) { results.each... } })`. Blackmwana pretty much answered it though :). Thanks blackmwana – Erick Tai Mar 19 '13 at 15:14