簡體   English   中英

骨干:設置父視圖的模型(不同於子模型)

[英]Backbone: Set the parent view's model (different from child model)

我有ParentChild觀點。 Child視圖將Parent事件擴展為:

    initialize : function() {
        // don't overwrite parent's events
        this.events = _.extend( {}, ExerciseRowView.prototype.events, this.events);
    },

但是, Parent期望有ParentModelChild期望有ChildModel ,因此,當事件傳遞給Parent ,模型就是ChildModel 如何將Parent模型設置為與Child模型不同?

謝謝!

這是要求的來源。

ParentView又名ExerciseRowView

var ExerciseRowView = Parse.View.extend( {
        tagName : 'div',
        className : 'exerciseWrapper',

        template : _.template(exerciseElement),

        events : {
            'click .icon_delete' : 'confirmDelete',
            'click .name' : 'showDetailsPopup'
        },

        confirmDelete : function() {
            var that = this;

            if(confirm("Are you sure you want to delete this exercise?")) {
                this.destroy({
                    success: function(exercise) {
                        // log the action
                        Log.add(Log.ACTION_EXERCISE_DELETED, exercise.get("name"));

                        that.$el.fadeOut();
                    }
                });
            }
        },

        showDetailsPopup : function() {
            (new ExerciseDetailsView({model: (this.model.constructor == Exercise ? this.model : this.model.get("exercise"))})).render();
        },

        // accept data as a parameter for workoutexercises
        render : function(data) {
            _.defaults(data, {
                exercise: this.model,
                Muscle : Muscle,
                Equipment : Equipment,
                Exercise : Exercise,
                Break : Break,
                HTMLHelper : HTMLHelper,
                User : User
            });
            $(this.el).html(this.template(data));
            return this;
        }
    });

ChildView又名WorkoutExerciseRowView

var WorkoutExerciseRowView = ExerciseRowView.extend( {      

        events : {
            "click .icon_randomize" : "changeToRandomExercise"
        },

        initialize : function() {
            // don't overwrite parent's events
            this.events = _.extend( {}, ExerciseRowView.prototype.events, this.events);
        },

        render: function() {
            // override the template data with workout exercise template data
            return ExerciseRowView.prototype.render.call(this, {
                workoutExercise : this.model,
                exercise : this.model.get("exercise"),
                workoutSection : this.model.get("section"),
                isEditable : true,
                number : this.options.number,
                WorkoutExercise : WorkoutExercise,
                WorkoutSection : WorkoutSection
            });
        },

        changeToRandomExercise : function(e) {
            // pick a random alternative exercise
            var newExerciseId;
            do {
                newExerciseId = _.keys(this.model.get("alternativeExercises"))[ Math.floor(Math.random() * _.keys(this.model.get("alternativeExercises")).length) ];
            } while(newExerciseId == this.model.get("exercise").id);

            // grab it
            var that = this;
            (new Parse.Query(Exercise)).get(newExerciseId, {
                success: function(exercise) {
                    // update the workout exercise
                    that.model.set("exercise", exercise);

                    // render it
                    that.render();
                }
            });
        }
    });

當前(如您所見),我測試以查看this.model.constructor == Exercise內部進行ExerciseRowView 如果不是,我知道我有一個WorkoutExercise ,里面是一個Exercise ,所以我使用this.model.get("exercise")

showDetailsPopup : function() {
            (new ExerciseDetailsView({model: (this.model.constructor == Exercise ? this.model : this.model.get("exercise"))})).render();
        },

不過,這似乎不是最干凈的解決方案。

我能想到的是您為每個視圖定義功能

父視圖

getExercise: function() {
  return this.model;
}

兒童觀

getExercise: function() {
  return this.model.get('exercise');
}

然后改變功能

showDetailsPopup: function() {
  (new ExerciseDetailsView({model: this.getExercise()})).render();
}

那個怎么樣?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM