Backbone如何编辑项目的视图

时间:2014-03-21 18:26:30

标签: jquery backbone.js collections views

我有两个收藏品stolovi和racuniTemp。我需要通过属性StoloviID在每个racuniTemp项目的stolovi中找到项目...我需要删除" a"从stolovi项目视图中标记并更改img src。我尝试了一百种不同的方式,没有任何成功。这就是我所拥有的:

<div id="stolovi" class="stolovi">  
        <script id="stolTemplate" type ="text/template">
            <div style="margin:3px;">
                <div class='wrapper'>  
                    <a href="#" data-id ="<%= StolID %>"><img id="<%= Opis %>" src="data:image/png;base64,<%= Slika %>"/></a>
                    <div class='description'>  
                         <p class='description_content'><%= Opis %></p>  
                    </div>  
                </div>
             </div>
         </script>    
    </div>  

App.Models.Stol = Backbone.Model.extend({
        idAttribute: "StolID",
        defaults:
            {
                StolID: 1,
                Opis: "Stol 1",
                Slika: ""
            },
        urlRoot: 'api/stolovi'
    });

    App.Collections.Stolovi = Backbone.Collection.extend({
        model: App.Models.Stol,
        url: 'api/stolovi',
        //    obrisiA: function(id) {
        //        filtered = this.filter(function(stol) {
        //            return stol.get("StolID") === id;
        //        });
        //    return new App.Collections.Stolovi(filtered);
        //},
        });


    App.Views.Stol = Backbone.View.extend({
        tagName: 'div',

        template: _.template( $('#stolTemplate').html() ),

        initialize: function () {

        },

        render: function (id) {
            //if (this.model.attributes.StolID = id) {
            //    this.children.find("img").attr('src', '../Images/xsoftLogo.png');

            //}
            $(this.el).html(this.template(this.model.toJSON()));
            return this;
        }
    });


//var promijeniSliku = function (view) {
//    view.$el.img.src = "~/Images/xsoftLogo.png";
//}

//var stoloviFilt = stolovi.obrisiA(model.attributes.StolID);
//var viewZauzetiStolovi = new App.Views.Artikli({ collection: stoloviFilt });
//viewZauzetiStolovi.each(promijeniSliku, this);

    var racuniTemp = new App.Collections.RacuniTemp();
    //var viewRacuniTemp = new App.Views.RacuniTemp({ collection: racuniTemp });
    racuniTemp.fetch({
        success: function () {
            //racuniTemp.each(prikaziRacune, this);
        }
    });

    //var stl = document.getElementById('Stol 1');
    //var pct= $stl.find("a").getAttribute('data-id', '24');
    //pct.find("img").attr("src", "~/Images/xsoftLogo.png");

感谢您提供任何帮助!!!

1 个答案:

答案 0 :(得分:0)

我以某种方式使用jquery解决了这个问题。我不确定这是解决这个问题的好方法。

 var slika = $('#stolovi #stolWrapper[data-id=' + model.attributes.StolID + '] img');
slika.attr('src', 'Images/account_and_control.png');
$('#stolovi #stolWrapper[data-id=' + model.attributes.StolID + ']').bind('click #stolWrapper', function () { return false; });