Skip to content

Commit 236c002

Browse files
author
pc-david\david.desmaisons
committed
First implementation of two-binding
1 parent 32c0e4e commit 236c002

File tree

5 files changed

+63
-58
lines changed

5 files changed

+63
-58
lines changed

examples/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616
<div id="main">
1717
<h1>Vue Dragable For</h1>
1818

19+
<h2>Dragable</h2>
1920
<div>
2021
<div v-dragable-for="element in list">{{element.name}}</div>
2122
</div>
2223

24+
<h2>Normal</h2>
25+
<div>
26+
<div v-for="element in list">{{element.name}}</div>
27+
</div>
28+
29+
<button @click="add">Add</button>
30+
<button @click="replace">Replace</button>
31+
2332
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
2433
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
2534
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>

examples/script/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@ var vm = new Vue({
44
list:[{name:"John"},
55
{name:"Joao"},
66
{name:"Jean"} ]
7+
},
8+
methods:{
9+
add: function(){
10+
this.list.push({name:'Juan'});
11+
},
12+
replace: function(){
13+
this.list=[{name:'Edgard'}]
14+
}
715
}
816
});

examples/src/vuedragablefor.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,43 @@
11
(function(){
22

3-
function copyMethod(target, source, preserve){
4-
for (var property in source) {
5-
if (!source.hasOwnProperty(property) || preserve.indexOf(property)!==-1)
6-
continue;
7-
8-
var value = source[property];
9-
if (typeof value !== "function")
10-
continue;
11-
12-
target[property] = function(){
13-
return value.apply(this, arguments);
14-
}
15-
}
16-
}
17-
183
var forDirective = Vue.directive('for');
19-
var clonedForDirective = _.clone(forDirective);
4+
var dragableForDirective = _.clone(forDirective);
5+
dragableForDirective.params = dragableForDirective.params.concat('root', 'options');
206

217
function mix(source, functions){
228
_.forEach(['bind', 'update', 'unbind'],function(value){
23-
var original = clonedForDirective[value];
24-
clonedForDirective[value] = function(){
9+
var original = source[value];
10+
source[value] = function(){
2511
functions[value].apply(this, arguments);
2612
return original.apply(this, arguments);
2713
};
2814
});
2915
}
3016

31-
mix(clonedForDirective, {
32-
bind : function () {
17+
mix(dragableForDirective, {
18+
bind : function () {
19+
var ctx = this;
3320
var option ={
3421
onEnd: function (evt) {
35-
evt.oldIndex; // element's old index within parent
36-
evt.newIndex; // element's new index within parent
3722
console.log(evt.oldIndex, evt.newIndex);
23+
var collection = ctx.collection;
24+
if (!!collection)
25+
collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] );
3826
}
3927
};
4028
this.sortable = new Sortable(this.el.parentElement, option);
4129
},
42-
update : function(){
43-
console.log(this.sortable)
30+
update : function (value){
31+
32+
if ((value!==null) && (!Array.isArray(value)))
33+
throw new Error('should received an Array');
34+
35+
console.log('update', value);
36+
this.collection = value;
4437
},
45-
unbind : () => console.log('unbind'),
38+
unbind : function (){}
4639
});
4740

48-
Vue.directive('dragable-for', clonedForDirective);
41+
Vue.directive('dragable-for', dragableForDirective);
4942

5043
})();

src/vuedragablefor.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
11
(function(){
22

3-
function copyMethod(target, source, preserve){
4-
for (var property in source) {
5-
if (!source.hasOwnProperty(property) || preserve.indexOf(property)!==-1)
6-
continue;
7-
8-
var value = source[property];
9-
if (typeof value !== "function")
10-
continue;
11-
12-
target[property] = function(){
13-
return value.apply(this, arguments);
14-
}
15-
}
16-
}
17-
183
var forDirective = Vue.directive('for');
19-
var clonedForDirective = _.clone(forDirective);
4+
var dragableForDirective = _.clone(forDirective);
5+
dragableForDirective.params = dragableForDirective.params.concat('root', 'options');
206

217
function mix(source, functions){
228
_.forEach(['bind', 'update', 'unbind'],function(value){
23-
var original = clonedForDirective[value];
24-
clonedForDirective[value] = function(){
9+
var original = source[value];
10+
source[value] = function(){
2511
functions[value].apply(this, arguments);
2612
return original.apply(this, arguments);
2713
};
2814
});
2915
}
3016

31-
mix(clonedForDirective, {
32-
bind : function () {
17+
mix(dragableForDirective, {
18+
bind : function () {
19+
var ctx = this;
3320
var option ={
3421
onEnd: function (evt) {
35-
evt.oldIndex; // element's old index within parent
36-
evt.newIndex; // element's new index within parent
3722
console.log(evt.oldIndex, evt.newIndex);
23+
var collection = ctx.collection;
24+
if (!!collection)
25+
collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] );
3826
}
3927
};
40-
this.sortable = new Sortable(this.el, option);
28+
this.sortable = new Sortable(this.el.parentElement, option);
29+
},
30+
update : function (value){
31+
32+
if ((value!==null) && (!Array.isArray(value)))
33+
throw new Error('should received an Array');
34+
35+
console.log('update', value);
36+
this.collection = value;
4137
},
42-
update : function (){},
4338
unbind : function (){}
4439
});
4540

46-
Vue.directive('dragable-for', clonedForDirective);
41+
Vue.directive('dragable-for', dragableForDirective);
4742

4843
})();

test/tmp/isrc.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)