Skip to content

Commit 781b9ea

Browse files
Improve unbind
1 parent 950f7d4 commit 781b9ea

File tree

8 files changed

+54
-96
lines changed

8 files changed

+54
-96
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ app/bower_components
66
test/bower_components
77
bower_components
88
examples/src
9-
test/tmp
9+
test/tmp
10+
/test/tmp
11+
/examples/src

build/vuedragablefor.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/complex.html

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@ <h1>Vue Dragable For</h1>
1818

1919
<a href="index.html">See basic example</a>
2020

21-
<h2>Dragable list 1</h2>
22-
<div>
23-
<div v-dragable-for="element in list1" options='{"group":"people"}'>{{element.name}}</div>
21+
<div class="drag">
22+
<h2>List 1 v-dragable-for</h2>
23+
<div class="dragArea">
24+
<div v-dragable-for="element in list1" options='{"group":"people"}'>{{element.name}}</div>
25+
</div>
26+
27+
<h2>List 2 v-dragable-for</h2>
28+
<div class="dragArea">
29+
<div v-dragable-for="element in list2" options='{"group":"people"}'>{{element.name}}</div>
30+
</div>
2431
</div>
2532

26-
<h2>Dragable list 2</h2>
27-
<div>
28-
<div v-dragable-for="element in list2" options='{"group":"people"}'>{{element.name}}</div>
33+
<div class="normal">
34+
<h2>List 1 v-for</h2>
35+
<div>
36+
<div v-for="element in list1" options='{"group":"people"}'>{{element.name}}</div>
37+
</div>
38+
39+
<h2>List 2 v-for</h2>
40+
<div>
41+
<div v-for="element in list2" options='{"group":"people"}'>{{element.name}}</div>
42+
</div>
2943
</div>
3044

3145
<button @click="add">Add</button>

examples/css/main.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.normal {
2+
background-color: grey;
3+
}
4+
.drag {
5+
background-color: green;
6+
}
7+
.dragArea{
8+
min-height: 10px;
9+
}

examples/index.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ <h1>Vue Dragable For</h1>
1818

1919
<a href="complex.html">See other example</a>
2020

21-
<h2>Dragable</h2>
22-
<div>
23-
<div v-dragable-for="element in list">{{element.name}}</div>
21+
<div class="drag">
22+
<h2>Dragable v-dragable-for</h2>
23+
<div class="dragArea">
24+
<div v-dragable-for="element in list">{{element.name}}</div>
25+
</div>
2426
</div>
2527

26-
<h2>Normal</h2>
27-
<div>
28-
<div v-for="element in list">{{element.name}}</div>
28+
<div class="normal">
29+
<h2>Normal v-for</h2>
30+
<div class="dragArea">
31+
<div v-for="element in list">{{element.name}}</div>
32+
</div>
2933
</div>
3034

3135
<button @click="add">Add</button>

examples/src/vuedragablefor.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/vuedragablefor.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,37 @@
1818
bind : function () {
1919
var ctx = this;
2020
var options = this.params.options;
21-
if (typeof options === "string")
22-
options = JSON.parse(options);
21+
options = (typeof options === "string") ? JSON.parse(options) : options;
2322
options = _.merge(options,{
2423
onUpdate: function (evt) {
2524
var collection = ctx.collection;
2625
if (!!collection)
2726
collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] );
2827
},
2928
onAdd: function (evt) {
30-
var itemEl = evt.item; // dragged HTMLElement
31-
var directive = evt.from.__directive; // previous list
32-
if (!directive)
33-
return;
34-
ctx.collection.splice(evt.newIndex, 0, directive.collection[evt.oldIndex]);
29+
var directive = evt.from.__directive;
30+
if ((!!directive) && (!!ctx.collection))
31+
ctx.collection.splice(evt.newIndex, 0, directive.collection[evt.oldIndex]);
3532
},
3633
onRemove: function (evt) {
34+
if (!!ctx.collection)
35+
ctx.collection.splice(evt.oldIndex, 1);
3736
}
3837
});
39-
var parent = this.el.parentElement;
38+
var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement;
4039
parent.__directive = this;
41-
console.log(options);
42-
//var option =
4340
this.sortable = new Sortable(parent, options);
4441
},
4542
update : function (value){
46-
47-
if ((value!==null) && (!Array.isArray(value)))
43+
if ((!!value) && (!Array.isArray(value)))
4844
throw new Error('should received an Array');
4945

50-
console.log('update', value);
5146
this.collection = value;
5247
},
53-
unbind : function (){}
48+
unbind : function (){
49+
this.sortable.destroy();
50+
}
5451
});
5552

5653
Vue.directive('dragable-for', dragableForDirective);
57-
5854
})();

test/tmp/isrc.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)