Laravel 4でタイムスタンプを読み取り可能な日付に変換するために設定したヘルパー関数があります。この関数は、ファイルStringEdit.php(「helpers」フォルダー内)で「getDate」と呼ばれます。私はこの機能が動作することを知っています。
データベースからデータを取得して div id にロードする jquery 呼び出しでこの関数を使用しようとしています (クリックするたびにさらにデータをロードします)。コードは次のとおりです。
<script type="text/javascript">
$(function() {
<?php $number_of_posts = 2; ?>;
<?php $_SESSION['posts_start'] = isset($_SESSION['posts_start']) ? $_SESSION['posts_start'] : $number_of_posts; ?>;
//<?php $_SESSION['posts_start'] = $_SESSION['posts_start'] ? $_SESSION['posts_start'] : $number_of_posts; ?>;
//<?php $_SESSION['posts_start'] = 2 ?>;
//var start = <?php echo $_SESSION['posts_start']; ?>;
var start = {{ Session::get('posts_start', 2) }};
var initialPosts = <?php echo Fanartist::friend_activity_json(0, $_SESSION['posts_start']); ?>;
//var initialPosts = <?php echo Fanartist::friend_activity_json(0, 2); ?>;
var desiredPosts = <?php echo $number_of_posts; ?>;
var template = '<tr>'
+'<td>'
+'<div class="friend_image">'
+'<img src="https://graph.facebook.com/fbid/picture" alt="" height="65" width="65" class="img-rounded">'
+'</div>'
+'</td>'
+'<td>'
+'<div class="friend_activity">'
+'<span class="activity_text"><span class="first_name"></span> <span class="last_name"></span> indicated that <span class="gender"></span> wants <a href="/artists/artist_id"><span class="stage_name"></span></a> to come to <span class="city"></span></span>'
+'<br><span class="activity_subtext"><span class="created_at"></span></span>'
+'</div>'
+'</td>'
+'</tr>';
var activity = $('#activity'),
// Element to load the posts
content = activity.find('.content'),
// the more button
more = activity.find('.more'),
// the post counter
counter = activity.find('.badge');
// Create alerts elements (Display Success or Failure)
var alerts = {
requestEmpty : $('<div class="alert alert-info">No more data</div>'),
requestFailure : $('<div class="alert alert-error">Could not get the data. Try again!</div>')
}
var progressElement = $('<div class="progress" style="margin-bottom:0"><div class="bar"></div></div>');
var progressBar = progressElement.find('.bar');
var postHandler = function(posts){
// Set the progress bar to 100%
progressBar.css('width', '100%');
// Delay the normal more button to come back for a better effect
window.setTimeout(function(){more.html('More <span class="caret"></span>')}, 500);
// insert childrens at the end of the content element
for (post in posts){
// Clone the element
var $post = $(template).clone();
$post.attr('id', 'post-' + posts[post].ID);
var $img = $post.find('div.friend_image').find('img');
$img.attr('src', $img.attr('src').replace('fbid', posts[post].fbid));
$img.attr('alt', posts[post].first_name);
var $spantext = $post.find('div.friend_activity').find('span.activity_text');
$spantext.html($spantext.html().replace('artist_id', posts[post].artist_id));
//$post.find('.fbid').html(posts[post].fbid);
$post.find('.first_name').html(posts[post].first_name);
$post.find('.last_name').html(posts[post].last_name);
$post.find('.city').html(posts[post].city);
$post.find('.gender').html(posts[post].gender == 'male' ? 'he' : 'she');
//$post.find('.artist_id').html(posts[post].artist_id);
$post.find('.stage_name').html(posts[post].stage_name);
$post.find('.created_at').html(posts[post].created_at);
content.append($post);
}
content.animate({
scrollTop: $('#post-' + posts[0].ID).offset().top + (content.scrollTop()- content.offset().top)
}, 200);
}
// place the initial posts in the page
postHandler(initialPosts);
// add the click event to the more button
more.click(function(){
// Set the progress bar to 0%
progressBar.css('width', '0%');
// remove the more button innerHTML and insert the progress bar
more.empty().append(progressElement);
// AJAX REQUEST
$.ajax({
url: "http://crowdtest.dev:8888/fans/setup_widget",
type: 'GET',
// We do not want IE to cache the result
cache: false,
data: {
'start': start,
'desiredPosts': desiredPosts
}
}).success(function (data, text) {
// parse the response (typeof data == String)
data = JSON.parse(data);
if (data.length > 0){
// Update the total number of items
start += data.length;
// Update the counter
counter.html(start);
// load items on the page
postHandler(data);
}else{
$alert = alerts.requestEmpty;
// insert the empty message
activity.prepend($alert);
// Set the progress bar to 100%
progressBar.css('width', '100%');
// Remove the more button
window.setTimeout(function(){more.remove()}, 500);
// remove the empty message after 4 seconds
window.setTimeout(function(){$alert.remove()}, 4000);
}
}).error(function (request, status, error) {
$alert = alerts.requestFailure;
// insert the failure message
activity.prepend($alert);
// Set the progress bar to 100%
progressBar.css('width', '100%');
// Delay the normal more button to come back for a better effect
window.setTimeout(function(){more.html('More <span class="caret"></span>')}, 500);
});
});
console.log(desiredPosts);
console.log(start);
console.log(initialPosts);
});
</script>
これは、div id「アクティビティ」を使用して html で呼び出されます。
<div id="activity">
<table class="table table-striped">
<thead>
<tr>
<th><div class="friend_image"></div></th>
<th><div class="friend_activity"></div></th>
</tr>
</thead>
<tbody class="content">
</tbody>
</table>
<button class="more btn btn-block">
More <span class="caret"></span>
</button>
</div>
created_at の部分を除いて、すべてが正しく出力されます。エラーが発生します:
SyntaxError: missing ) after argument list
行を参照する:
$post.find('.created_at').html(posts[post].StringEdit::getDate(created_at));
この関数を呼び出して、jquery 内のデータベースから created_at エンティティを編集する方法を知っていますか? ご協力ありがとうございました。