如何更新模板视图或json数据,以便在backbone.js中重新呈现模板视图

如何更新模板视图或json数据,以便在backbone.js中重新呈现模板视图,backbone.js,Backbone.js,我想更新视图,当我点击follow按钮时,follow按钮的id是btn follow。 我想更新ui if模板,当我点击follow按钮时,我会更新该模板,如果控制台数据中的值为true,follow按钮应在“follow”中更改,如果控制台中的值为false,则按钮标题应为unfollow。如何更新视图或如何更新joson数据并重新渲染模板 我的视图代码在这里 spine.module("communityApp", function (communityApp, App, Backbone

我想更新视图,当我点击follow按钮时,follow按钮的id是btn follow。 我想更新ui if模板,当我点击follow按钮时,我会更新该模板,如果控制台数据中的值为true,follow按钮应在“follow”中更改,如果控制台中的值为false,则按钮标题应为unfollow。如何更新视图或如何更新joson数据并重新渲染模板

我的视图代码在这里

spine.module("communityApp", function (communityApp, App, Backbone, Marionette, $, _) {

    // Load template

    var pforumTemplateHtml = App.renderTemplate("pforumTemplate", {}, "communityModule/tabContainer/pforum");
    // Define view(s)
    communityApp.Views.pforumView = Marionette.ItemView.extend({
        template: Handlebars.compile($(pforumTemplateHtml).html()),
        tagName: "li",
        onRender: function () {
            this.object = this.model.toJSON();
        },


        events: {
            "click .btn-comment" : "showComments",
            "click #recent-btn": "recent",
            "click #my-posts": "myposts",
            "click #popular-btn": "popular",
            "click #follow-btn": "follow",
            "click #my-posts": "LeftLinks",
            "click #popular-btn": "LeftLinks",
            "click .btn-follow": "activityBtn",
            "click #like-btn" : "activityBtn",
            "click #post-comments-btn": "showCommentEiditor"

        },  

        postcomments : function ()

    {
    $("#recent-post-main-container").hide();
    $("#recent-post-main-container2").show();

    },

    showCommentEiditor : function (){

    $(".comment-popup-container").show();
    $(".comment-txt-area").val('');

    },

    showPforumTab : function ()
    {
    $("#recent-post-main-container2").show();
    $("#recent-post-main-container").hide();
    },

    showComments : function(){

    $("#loading").show();
    $(".tab-pane").hide();
    $(".left-content").hide();
    $("#recent-post-main-container2").show();
    $(".left-content-commentEditor").show();
    //$(".comm-tab-content-container").css('height','200px');
    $(".comment-txt-area").val('');
    $(".left-content-comment").show();
    },

     hideCommentPopup : function ()
     {
        $("#public-question-comment").hide();
         },
    // Show Loading sign
    showLoading : function () {
    $('#loading').show();
    },

    // UnLoading Function
    hideLoading : function (){
    $('#loading').hide();
    },

    // Add New Event Function
     addEvent : function()
    {
    //$("#name").val(getBackResponse.user.FullName);
    //$("#phone").val(getBackResponse.user.CellPhone);
    //$("#email").val(getBackResponse.user.UserName);
    $(".overly.addevent").show();
    $('#lang').val(lat);
    $('#long').val(long);
    document.getElementById("my-gllpMap").style.width = "100%";
    var my_gllpMap = document.getElementById("my-gllpMap");
    google.maps.event.trigger( my_gllpMap, "resize" );
    },

     setValues : function(key,value)
    {
    window.localStorage.setItem(key,value);     
    },

    getValues : function (key) 
    {
    return window.localStorage.getItem(key);        
    },

    closeAddEvent:function ()
    {
    $(".overly.addevent").hide();
    },

    // Show Over lay
    showOverly:function ()
    {
    $('.overly-right-tab').show();
    },

    // Hide Loading sign
     hideOverly : function()
    {
    $('.overly-right-tab').hide();
    },

    enlargeImage : function ()
    {
        $('#image').css('width','212px');
        $('#image').css('height','150px');
        },

         activityBtn: function (e) {
           var elem = $(e.target);
            if (elem.hasClass('inactive')) {
                return false;
            }
            var activity = elem.attr('name');
            switch (activity) {
                case "like-Button":
                    var _this = $.extend({},this,true);
                    _this.activity = 'like-Button';
                    this.activityBtnSubmit.call(_this);
                    break;
                    //
                    case "follow-button":
                    var _this = $.extend({},this,true);
                    _this.activity = 'follow-button';
                    this.activityBtnSubmit.call(_this);
                    break;


            }
        },
        //For Like Post
        activityBtnSubmit: function (modalThis) {
        //   var o = (this.parentThis) ? this.parentThis.object : this.object;
            //var o = "52fa2ccc9bca9ac90c000051";
            var func = _.bind(function () {
                //var hmObj = new MessageApp.Controllers.hmAlertsController();
                //hmObj.init();
            }, this);
           switch (this.activity) {

            case "like-Button":

            App.ids = null;
            App.ids2 = null;
            App.ids = this.object.id;    
            App.ids2 = this.object.iLiked;

              if(App.ids2 === true) {
                 App.action = 0;
              }
                 else if(App.ids2 === false) {
              App.action = 1;
              }


                   var data = {

                        id: this.object.id,
                        iLiked:App.action,
                        sessionToken:loginUser.sessionToken,

                    };
                    $.when(App.request('alertActivity:entities', {
                        origin: 'pforum',
                        id: this.object.id,
                        iLiked:(App.action),
                        sessionToken:loginUser.sessionToken,
                        //value : value,
                        dataToSend: JSON.stringify(data),
                        activity:this.activity,

                    }))

                    .then(func);
                    App.ids1 = (data.id);

                    break;

             case "follow-button":

             App.ids = null;
            App.ids2 = null;

            App.ids = this.object.UserId;    
            App.ids2 = this.object.iFollow;

                if(App.ids2 === true) {
                    // $(".btn-follow").html("UN-FOLLOW");
                     App.action = 0;

                    }
                else if(App.ids2 === false) {
                    //$(".btn-follow").html("FOLLOW");
                    App.action = 1;

                    }

                     if (App.ids) {
                         alert (App.ids);

                     $(".btn-follow").html("UN-FOLLOW");
                     //App.action = 0;

                    }
                else 
                 {
                    $(".btn-follow").html("FOLLOW");
                    //App.action = 1;

                    }

                   var data = {

                        id: this.object.UserId,
                        iFollow:App.action,
                        sessionToken:loginUser.sessionToken,

                    };
                    $.when(App.request('alertActivity:entities', {
                        origin: 'pforum',
                        id: this.object.UserId,
                        iFollow:(App.action),
                        sessionToken:loginUser.sessionToken,
                        //value : value,
                        dataToSend: JSON.stringify(data),
                        activity:this.activity,

                    }))

                    .then(func);
                    App.ids1 = (data.UserId);

                    break;

                     }

                   return true;



        }

        });

        var RowView = Backbone.View.extend({  
         events: {
             "click .btn-follow": function() {console.log(this.model.get("name"));}
         },
         initialize: function(){
           this.model.on('change', this.render, this);
         },
         render: function() {
             var html=_.template(rowTemplate,this.model.toJSON());
             this.setElement( $(html) );
             return this;

         },
     });




    // define collection views to hold many communityAppView:
    communityApp.CollectionViews.pforumCollectionViews = Marionette.CollectionView.extend({
        tagName: "ul",
        itemView: communityApp.Views.pforumView
    });

});
<div>
    <div class="comm-tab-row">
        <div class="post-left-panel">

            <div class="post-image-container">
                <img src="{{UserImageURL}}" alt="" class="post-image"  /></br>

            {{#if iLiked}}
            <img src="images/myCommunity/liked@2x.png" width="20" height="19" id="like-btn" name = "like-Button" >
            {{else}}
             <img src="images/myCommunity/like@2x.png" width="20" height="19" id="like-btn" name = "like-Button" >
            {{/if}}


        ({{NumLikes}})    

            </div>
        </div>

        <div class="post-body">
            <h5 class="comm-tab-heading">
                <span class="navigate-screen" id="{{Id}}" style="text-decoration:underline;">
                    {{UserName}}
                </span>
                <span>
                     - &nbsp;
                </span>
                <span>
                   {{format_date Time ""}}
                </span>
            </h5>
            {{Message}}
                 </div>

            <div class="comm-right-panel">
            {{#if iFollow}}
            <a href="javascript:void(0);" class="btn-follow" name = "follow-button">UN-FOLLOW</a>
            {{else}}
             <a href="javascript:void(0);" class="btn-follow" name = "follow-button">FOLLOW</a>
            {{/if}}



                <a href="javascript:void(0);" class="btn-comment" name = "comment-button">{{NumComments}} - COMMENT</a>

        </div>
我的模板代码在这里

spine.module("communityApp", function (communityApp, App, Backbone, Marionette, $, _) {

    // Load template

    var pforumTemplateHtml = App.renderTemplate("pforumTemplate", {}, "communityModule/tabContainer/pforum");
    // Define view(s)
    communityApp.Views.pforumView = Marionette.ItemView.extend({
        template: Handlebars.compile($(pforumTemplateHtml).html()),
        tagName: "li",
        onRender: function () {
            this.object = this.model.toJSON();
        },


        events: {
            "click .btn-comment" : "showComments",
            "click #recent-btn": "recent",
            "click #my-posts": "myposts",
            "click #popular-btn": "popular",
            "click #follow-btn": "follow",
            "click #my-posts": "LeftLinks",
            "click #popular-btn": "LeftLinks",
            "click .btn-follow": "activityBtn",
            "click #like-btn" : "activityBtn",
            "click #post-comments-btn": "showCommentEiditor"

        },  

        postcomments : function ()

    {
    $("#recent-post-main-container").hide();
    $("#recent-post-main-container2").show();

    },

    showCommentEiditor : function (){

    $(".comment-popup-container").show();
    $(".comment-txt-area").val('');

    },

    showPforumTab : function ()
    {
    $("#recent-post-main-container2").show();
    $("#recent-post-main-container").hide();
    },

    showComments : function(){

    $("#loading").show();
    $(".tab-pane").hide();
    $(".left-content").hide();
    $("#recent-post-main-container2").show();
    $(".left-content-commentEditor").show();
    //$(".comm-tab-content-container").css('height','200px');
    $(".comment-txt-area").val('');
    $(".left-content-comment").show();
    },

     hideCommentPopup : function ()
     {
        $("#public-question-comment").hide();
         },
    // Show Loading sign
    showLoading : function () {
    $('#loading').show();
    },

    // UnLoading Function
    hideLoading : function (){
    $('#loading').hide();
    },

    // Add New Event Function
     addEvent : function()
    {
    //$("#name").val(getBackResponse.user.FullName);
    //$("#phone").val(getBackResponse.user.CellPhone);
    //$("#email").val(getBackResponse.user.UserName);
    $(".overly.addevent").show();
    $('#lang').val(lat);
    $('#long').val(long);
    document.getElementById("my-gllpMap").style.width = "100%";
    var my_gllpMap = document.getElementById("my-gllpMap");
    google.maps.event.trigger( my_gllpMap, "resize" );
    },

     setValues : function(key,value)
    {
    window.localStorage.setItem(key,value);     
    },

    getValues : function (key) 
    {
    return window.localStorage.getItem(key);        
    },

    closeAddEvent:function ()
    {
    $(".overly.addevent").hide();
    },

    // Show Over lay
    showOverly:function ()
    {
    $('.overly-right-tab').show();
    },

    // Hide Loading sign
     hideOverly : function()
    {
    $('.overly-right-tab').hide();
    },

    enlargeImage : function ()
    {
        $('#image').css('width','212px');
        $('#image').css('height','150px');
        },

         activityBtn: function (e) {
           var elem = $(e.target);
            if (elem.hasClass('inactive')) {
                return false;
            }
            var activity = elem.attr('name');
            switch (activity) {
                case "like-Button":
                    var _this = $.extend({},this,true);
                    _this.activity = 'like-Button';
                    this.activityBtnSubmit.call(_this);
                    break;
                    //
                    case "follow-button":
                    var _this = $.extend({},this,true);
                    _this.activity = 'follow-button';
                    this.activityBtnSubmit.call(_this);
                    break;


            }
        },
        //For Like Post
        activityBtnSubmit: function (modalThis) {
        //   var o = (this.parentThis) ? this.parentThis.object : this.object;
            //var o = "52fa2ccc9bca9ac90c000051";
            var func = _.bind(function () {
                //var hmObj = new MessageApp.Controllers.hmAlertsController();
                //hmObj.init();
            }, this);
           switch (this.activity) {

            case "like-Button":

            App.ids = null;
            App.ids2 = null;
            App.ids = this.object.id;    
            App.ids2 = this.object.iLiked;

              if(App.ids2 === true) {
                 App.action = 0;
              }
                 else if(App.ids2 === false) {
              App.action = 1;
              }


                   var data = {

                        id: this.object.id,
                        iLiked:App.action,
                        sessionToken:loginUser.sessionToken,

                    };
                    $.when(App.request('alertActivity:entities', {
                        origin: 'pforum',
                        id: this.object.id,
                        iLiked:(App.action),
                        sessionToken:loginUser.sessionToken,
                        //value : value,
                        dataToSend: JSON.stringify(data),
                        activity:this.activity,

                    }))

                    .then(func);
                    App.ids1 = (data.id);

                    break;

             case "follow-button":

             App.ids = null;
            App.ids2 = null;

            App.ids = this.object.UserId;    
            App.ids2 = this.object.iFollow;

                if(App.ids2 === true) {
                    // $(".btn-follow").html("UN-FOLLOW");
                     App.action = 0;

                    }
                else if(App.ids2 === false) {
                    //$(".btn-follow").html("FOLLOW");
                    App.action = 1;

                    }

                     if (App.ids) {
                         alert (App.ids);

                     $(".btn-follow").html("UN-FOLLOW");
                     //App.action = 0;

                    }
                else 
                 {
                    $(".btn-follow").html("FOLLOW");
                    //App.action = 1;

                    }

                   var data = {

                        id: this.object.UserId,
                        iFollow:App.action,
                        sessionToken:loginUser.sessionToken,

                    };
                    $.when(App.request('alertActivity:entities', {
                        origin: 'pforum',
                        id: this.object.UserId,
                        iFollow:(App.action),
                        sessionToken:loginUser.sessionToken,
                        //value : value,
                        dataToSend: JSON.stringify(data),
                        activity:this.activity,

                    }))

                    .then(func);
                    App.ids1 = (data.UserId);

                    break;

                     }

                   return true;



        }

        });

        var RowView = Backbone.View.extend({  
         events: {
             "click .btn-follow": function() {console.log(this.model.get("name"));}
         },
         initialize: function(){
           this.model.on('change', this.render, this);
         },
         render: function() {
             var html=_.template(rowTemplate,this.model.toJSON());
             this.setElement( $(html) );
             return this;

         },
     });




    // define collection views to hold many communityAppView:
    communityApp.CollectionViews.pforumCollectionViews = Marionette.CollectionView.extend({
        tagName: "ul",
        itemView: communityApp.Views.pforumView
    });

});
<div>
    <div class="comm-tab-row">
        <div class="post-left-panel">

            <div class="post-image-container">
                <img src="{{UserImageURL}}" alt="" class="post-image"  /></br>

            {{#if iLiked}}
            <img src="images/myCommunity/liked@2x.png" width="20" height="19" id="like-btn" name = "like-Button" >
            {{else}}
             <img src="images/myCommunity/like@2x.png" width="20" height="19" id="like-btn" name = "like-Button" >
            {{/if}}


        ({{NumLikes}})    

            </div>
        </div>

        <div class="post-body">
            <h5 class="comm-tab-heading">
                <span class="navigate-screen" id="{{Id}}" style="text-decoration:underline;">
                    {{UserName}}
                </span>
                <span>
                     - &nbsp;
                </span>
                <span>
                   {{format_date Time ""}}
                </span>
            </h5>
            {{Message}}
                 </div>

            <div class="comm-right-panel">
            {{#if iFollow}}
            <a href="javascript:void(0);" class="btn-follow" name = "follow-button">UN-FOLLOW</a>
            {{else}}
             <a href="javascript:void(0);" class="btn-follow" name = "follow-button">FOLLOW</a>
            {{/if}}



                <a href="javascript:void(0);" class="btn-comment" name = "comment-button">{{NumComments}} - COMMENT</a>

        </div>


{{{#如果我喜欢} {{else} {{/if} ({{NumLikes}}) {{UserName}} - {{format_date Time”“}} {{Message}} {{{#如果下面} {{else} {{/if}



您可以向视图的模型添加新的
布尔值
。然后,可以在模板中添加条件,以确定要渲染的按钮

<% if(isFollowing){ %> 
    <button> Unfollow </button>
<% } else { %> 
    <button> Follow</button>
<% } %>

感谢Haraarat的回复,我认为您建议的代码仅在加载模板时有效,实际上它在加载模板时有效,我希望它在运行时按“跟随时钟”按钮时有效,因此我如何重新呈现模板,以便它能够显示更新的数据。谢谢,没问题!我已经更新了关于如何重新渲染视图的答案。ohhh。它不工作,没有重新呈现页面,没有采取任何行动。实际上,我想重新呈现和更新json数据,以便在模板上查看更新的数据,还有其他想法吗?@Khalil:您是否尝试过将此答案中的下划线模板语法转换为Handlebar语法?