0

I have a page called: contact_profile.php that uses jquery .load to load info into a div.

$('#json_comments').load('json_comments_content.php');

json_comments_content.php looks like this:

<?php
require_once 'core/init.php';

$user = new User();
if(!$user->isLoggedIn()) {
    Redirect::to('login.php');
}

$contact = new Contact();

//check to make sure there is current contact selected, otherwise redirect to index. This helps when you deselect a contact from menu bar while on contact page.
if (!($contact->isSelected())) {
    Redirect::to('index.php');
}

?>
<div class="items general-item-list">
    <div class="item">
        <div class="item-head">
            <div class="item-details">
                <img class="item-pic" data-field="pic_url" src="">
                <a href="" class="item-name primary-link" data-field="username"></a>
                <span class="item-label" data-field="datetime" data-value="" data-pk=""></span>
            </div>
        </div>
        <div class="item-body"><a href="#" id="comment" class="can_possibly_edit" data-type="textarea" data-field="comment" data-pk="" data-value="" data-placement="right" data-original-title="Enter a comment."></a></div>
    </div>
</div> 
    <a href="#" class="items-load">Load More...</a> 

<script type="text/javascript">     

$(document).ready(function() {

    $('.comments_data').loadmore({
        source: 'json_comments.php',
        step: 15,
        userid: '<?php echo $user->data()->id; ?>'
    });

    //on load, disabled the comments editable info on page load so it looks better.
    $('#json_comments a').attr("data-disabled", "true");


    $.fn.editable.defaults.mode = 'inline';


});

</script>

I am using a custom plugin called 'loadmore' that will load more data on my page from a mysql database. It works fine.

However, I have to use the following code for the data that is supplied by the loadmore plugin:

                $('.edit_me').editable({
                emptytext: ".....",
                url: "ajax_xeditable_update.php?table=comments",
            });

That code is using the X-Editable plugin for jQuery: http://vitalets.github.io/x-editable/

If I place the code in the loaded content's page inside the document.ready function, it never gets called!

Here's what my loadmore plugin looks like. If the code for X-Editable is placed there it will work properly. It would be better to have the code placed in the loaded page and NOT in the plugin - that way the plugin can stay generic.

Hope I was clear on my problem.

Here's the loadmore custom plugin:

(function ($) {
    "use strict";

    $.fn.loadmore = function(options) {
            var self = this,

            settings = $.extend({
                source: '',
                step: 2,
                userid: '',

            }, options),

            stepped = 1,
            item = self.find('.item'),
            items = self.find('.items'),

            finished = function() {
                // hide the load more button
                self.find('.items-load').remove();
            },

            append = function(value) {
                var name, part, id, userid, canedit;

                item.remove();

                for(name in value) {
                    if(value.hasOwnProperty(name)) {

                        id = value['id'];
                        userid = value['user_id'];
                        part = item.find('*[data-field="' + name +'"]');
                        //find anything that has a can edit class and then add the general editable class for x-editable to work. 
                        canedit = item.find(".can_possibly_edit");

                        if(part.length){

                             part.text(value[name]);

                            //add the value to an image if there is one for x-editable to work.
                            if($(part).is("img")) {
                                 $(part).attr("src", value[name]);
                             }

                            //only make the current user's stuff editable
                            if(settings.userid == userid ) {
                                //add edit_me to the classes so x=editable can work.   but then remove edit_me and the editable class so x-editable doesn't work for data that doesn't belong to the user(found in the else clause below).
                                $(canedit).addClass('edit_me editable editable-pre-wrapped editable-click editable-disabled');
                                $(canedit).attr('data-value', value[name]);
                                //there must be an id field in the json so it can be assigned to the primary key for x-editable to work.
                                $(canedit).attr('data-pk', id);

                            } else {
                                //remove hyperlink stuff and just leave the text to view only.
                                $(canedit).removeClass('edit_me editable');
                            }

                        }
                    }
                }

                item.clone().appendTo(items);

                //this works if it's placed here only!  

                $('.edit_me').editable({
                    emptytext: ".....",
                    url: "ajax_xeditable_update.php?table=comments",
                });

            },

            load = function(start, count) {
                $.ajax({
                    url: settings.source,
                    type: 'get',
                    dataType: 'json',
                    data: {start: start, count: count},
                    success: function(data) {
                        var items = data.items;

                        if(items.length) {
                            $(items).each(function(index, value) {
                                append(value);
                            });

                            stepped = stepped + count;
                        }

                        if(data.last === true) {
                            finished();
                        }
                    }
                });

            };

        if(settings.source.length) {
            self.find('.items-load').on('click', function(){
                load(stepped, settings.step);
                return false;
            });

            load(1, settings.step);
        } else {
            console.log('Source required for loadmore.');
        }

    };

}(jQuery))

It's almost like on the loaded page: json_comments_content.php I need to run the loadmore plugin on document.ready and THEN once the loadmore has been completed, return back to the page and run:

$('.edit_me').editable({
                emptytext: ".....",
                url: "ajax_xeditable_update.php?table=comments",
            });

Not sure if it matters, but the loadmore script is included on my main page from: 'js/loadmore.js'. It's in a subdirectory.

Alex Filatov
  • 2,232
  • 3
  • 32
  • 39
chris.cavage
  • 759
  • 1
  • 13
  • 35
  • Search SO for "execute script jquery load" for example http://stackoverflow.com/questions/4619668/executing-script-inside-div-retrieved-by-ajax – mplungjan Mar 20 '15 at 13:59
  • On the page that's loading the content, I tried the following but no go. – chris.cavage Mar 20 '15 at 18:19

0 Answers0