1

exercisesobservableArray 内に配列がありworkoutsます。そこからアイテムを削除する機能を追加したいと思います。それをobservableArrayにしようとしました:

self.exercises = ko.observableArray();

アイテムを削除する機能を追加しました:

self.removeExercise = function() {
        self.exercises.remove(this);
    };

しかし、何も起こりません。同じ関数がworkouts親配列で機能しますが。これに加えて、配列内のアイテムを編集するための簡単な関数があります。これは次のようになります。

this.edit = function() { this.editing(true) }

そして、それを私のビューにバインドしてみてください:

<span data-bind="text: name, click: edit"> </span>

しかし、うまくいきません。問題はどこにありますか?

これが私の見解です

<div class="content">
    <ul data-bind="foreach: workouts">
        <li>
            <span data-bind="text: name, click: edit"> </span>
                 <a href="#" data-bind="click: $parent.removeWorkout">Remove</a>
             <ul data-bind="foreach: exercises">
                 <li>
                     <span data-bind="text: name"> </span>
                     <a href="#" data-bind="click: $parent.removeExercise">remove</a>
                 </li>
             </ul>
        </li>
    </ul>   
</div>

そしてViewModel:

function AppViewModel() {   
    var self = this;

    self.workouts = ko.observableArray([
    {name: "Workout1", exercises:[
        { name: "Exercise1.1" },
        { name: "Exercise1.2" },
        { name: "Exercise1.3" }
    ]},
]);

    self.exercises = ko.observableArray();

    self.removeWorkout = function() {
        self.workouts.remove(this);
    };
    self.removeExercise = function() {
        self.exercises.remove(this);
    };

    this.editing = ko.observable(false);
    this.edit = function() { this.editing(true) }    
}

ko.applyBindings(new AppViewModel);

jsfiddle の例を次に示します: http://jsfiddle.net/c9fQB/

ありがとう!

4

1 に答える 1

1

bindingContext は removeExercise の $root である必要があり、演習は observableArray である必要があります。これを試してください:http://jsfiddle.net/Ag8rr/

オブザーバブル コード:

    self.exercises = ko.observableArray([
        { name: "Exercise1.1" },
        { name: "Exercise1.2" },
        { name: "Exercise1.3" }
    ]);    

    self.workouts = ko.observableArray([
    {name: "Workout1", exercises:self.exercises},
]);
于 2013-10-23T12:15:17.873 に答える