我有一些PHP代码,我想在我的Backbone App中实现。我使用PHP Framework Codeigniter作为Backend和Restful API来使这两个框架相互通信。
所以我的php代码看起来像这样:
<?php
require(APPPATH.'libraries/REST_Controller.php');
class social_feeds extends CI_Controller
{
function instagram($artist_id , $user_name = '')
{
if($user_name=='')
{
exit;//redirect('');
}
$user_search_data = $this->instagram_api->userSearch($user_name);
if(isset($user_search_data->data))
{
if(count($user_search_data->data) != 0)
{
$array_users = $user_search_data->data;
foreach($array_users as $users)
{
if($users->username== $user_name )
{
$user_recent_data = $this->instagram_api->getUserRecent($users->id);
//print_r($user_recent_data);
if(!isset($user_recent_data->meta->error_message))
{
if(is_array($user_recent_data->data))
{
$counter = 0;
foreach($user_recent_data->data as $feed_data)
{
//print_r($feed_data);
/*$image = array('src' => $feed_data->images->standard_resolution->url);
if(count($feed_data->tags) > 0)
{
foreach($feed_data->tags as $tag)
{
echo $tag;
}
}*/
$array[$counter]['title'] = $feed_data->images->standard_resolution->url;
$array[$counter]['description'] = $feed_data->caption->text;
$array[$counter]['pubDate'] = $feed_data->caption->created_time;
$array[$counter]['link'] = $feed_data->link;
$array[$counter]['type'] = 'Instagram';
++$counter;
if($counter==5)
{
// break;
}
//print_r($array);
}
$result = json_encode($array);
echo $result;
exit;
}
}
}
else
{
continue;
}
}
}
}
}
}
?>
我的Backbone View到目前为止看起来像这样:
define([
'app',
'backbone'
],
function (App, Backbone) {
// Create a new module.
var Instagram = App.module();
// Create view
Instagram.View = Backbone.View.extend({
template: 'instagram',
initialize: function() {
this.listenTo(this.collection, 'all', this.render)
},
serialize: function() {
return this.collection ? this.collection.toJSON() : [];
}
});
Instagram.InstagramCollection = Backbone.Collection.extend({
url: function() {
return './index.php/api/social_feeds/instagram/' + this.artist_id;
}
});
return Instagram;
});
在我的MainView中,我进行了获取:
var instagramCollection = new Instagram.InstagramCollection();
instagramCollection.artist_id = this.artist_id;
this.insertView('.artistSocialUpdates', new Instagram.View({collection: instagramCollection}));
instagramCollection.fetch();
然后我的HTML模板如下:
{{#each this}}
<p>{{title}}</p>
<p>{{description}}</p>
<p>{{pubDate}}</p>
<p>{{link}}</p>
{{/each}}
但显然这不起作用......
是否有人可以给我一个提示或告诉我我可能会做什么或我做错了什么?