こんにちは、私はBackbone.jsの新人です。コレクションをRESTfulpythongoogleアプリエンジンベースのバックエンドで動作させようとしています。サーバーで使用されるコードは次のとおりです。
def getJson():
tweets_out = []
tweets = getModTweets()
for tweet in tweets:
temp = {}
temp["screen_name"] = tweet.screen_name
temp["text"] = tweet.text
temp["profile_image_url"] = tweet.profile_image_url
temp["id"] = tweet.id
temp["created_at_time"] = tweet.created_at_time
temp["created_at_epoch"] = tweet.created_at_epoch
temp["time_lapsed"] = tweet.time_lapsed
tweets_out.append(temp)
a = json.dumps(tweets_out)
return a
生成されるJSONは次のとおりです。
[{"screen_name": "spolsky", "text": "@greeleygeek I'm going to start doing that", "profile_image_url": "http://a0.twimg.com/profile_images/1770014670/image1327118364_normal.png", "time_lapsed": "3 days ago", "created_at_epoch": 1346464678, "created_at_time": "2012-9-1 01:57:58", "id": 15948437}, {"screen_name": "spolsky", "text": "@phlix google analytics.", "profile_image_url": "http://a0.twimg.com/profile_images/1770014670/image1327118364_normal.png", "time_lapsed": "4 days ago", "created_at_epoch": 1346439185, "created_at_time": "2012-8-31 18:53:05", "id": 15948437}, {"screen_name": "spolsky", "text": "@AdityaAthalye we make it up in volume!", "profile_image_url": "http://a0.twimg.com/profile_images/1770014670/image1327118364_normal.png", "time_lapsed": "4 days ago", "created_at_epoch": 1346425278, "created_at_time": "2012-8-31 15:01:18", "id": 15948437}]
次の2つを使用して、コレクションに次の2つの方法でデータを入力しようとしましたが、成功しませんでした。
var Tweet = Backbone.Model.extend({
urlRoot : '/tweets.json'
});
var Tweets = Backbone.Collection.extend({
model: Tweet,
url: '/bulktweets.json'
});
var tweets2 = new Tweets();
//method 1
tweets2.reset(JSON STRING LISTED ABOVE);
//Yes tweets2.reset([{"screen_name": "spolsky", "text": "@greeleygeek I'm going to start doing that", "profile_image_url": "http://a0.twimg.com/profile_images/1770014670/image1327118364_normal.png", "time_lapsed": "3 days ago", "created_at_epoch": 1346464678, "created_at_time": "2012-9-1 01:57:58", "id": 15948437}, {"screen_name": "spolsky", "text": "@phlix google analytics.", "profile_image_url": "http://a0.twimg.com/profile_images/1770014670/image1327118364_normal.png", "time_lapsed": "4 days ago", "created_at_epoch": 1346439185, "created_at_time": "2012-8-31 18:53:05", "id": 15948437}, {"screen_name": "spolsky", "text": "@AdityaAthalye we make it up in volume!", "profile_image_url": "http://a0.twimg.com/profile_images/1770014670/image1327118364_normal.png", "time_lapsed": "4 days ago", "created_at_epoch": 1346425278, "created_at_time": "2012-8-31 15:01:18", "id": 15948437}
]);
//method 2
tweets2.fetch();
オーバーフローした人が私を正しい方向に向ける可能性はありますか?前もって感謝します。
更新: サーバーと通信できますが、コレクション全体が最初の要素だけで解析されるわけではありません。
コレクション、つまり、tweets2.lengthが予想される5ではなく1の長さを返すのはなぜですか?誰かが光を当てることができるでしょうか?