ng绑定$ swipe时单击不触发

时间:2014-02-09 02:06:34

标签: angularjs jquery-mobile swipe angularjs-ng-click

我对绑定到$ swipe的元素有一个ngClick指令。 ngClick不会触发(它在添加$ swipe之前触发)。

我正在使用jQuery mobile与AngularJS结合使用。

有趣的是,我的诊断显示滑动事件,其开始和结束相同 - 似乎与最小滑动距离相矛盾,但可能正在调用取消功能。可能我可以使用取消功能找出点击发生的位置,但我觉得我不应该这样做。

该网站可在http://skimobile.cbdweb.net

查看

HTML

<div id="matrix" data-role="page" ng-controller="matrixCtrl" ng-init="init()">
    <div data-role="header">
        <?php echo CHtml::encode($this->configs['SITENAME']); ?> Bookings
    </div>

    <div data-role="content">
        <div class="lodgename noborder">&nbsp;</div>
        <div class="oneday onemonth" ng-repeat="m in months" ng-style="callMonthstyle(m)" ng-class="$last ? 'lastcol' : ''" on-finish-days>
            {{monthNames[m.month]}} {{m.year}}
        </div>
        <br clear="both" />
        <div class="lodgename noborder">&nbsp;</div>
        <div class="oneday" ng-style="callDaystyle()" ng-class="$last ? 'lastcol' : ''" ng-repeat="d in dates">
            {{d.getDate()}}
        </div>
        <br clear="both" />
        <div ng-repeat="lodge in data.lodges">
            <div class="lodgename" ng-class="$last ? 'lastrow' : ''">{{lodge.lodgetitle}}</div>
            <div class="oneday" ng-style="callDaystyle()" ng-class="($last ? 'lastcol' : '') + ' ' + ($parent.$last ? 'lastrow' : '')" ng-repeat="d in dates" ng-click="showDate(lodge, d)">
                &nbsp;
            </div>
            <br clear="both" />
        </div>

        <div ng-show="data.debug" style="margin-top: 20px;"
            <ul>
                <li>
                    move = {{swipe.move}}
                </li>
                <li>
                   start = {{swipe.start}}
                </li>
                <li>
                    end = {{swipe.end}}
                </li>
                <li>
                    scope.startDay = {{startDay}}
                </li>
                <li>
                    daysMoved = {{swipe.daysMoved}}
                </li>
                <li>
                    daysFinished = {{nDaysFinished}}
                </li>
            </ul>
        </div>


        <ul>
            <?php foreach(Yii::app()->params['lodges'] as $lodgecode=>$lodge) {
                echo "<li>" . $lodgecode . " = " . $lodge['lodgetitle'] . "</li>";
                $lci = $this->lodgeconfigs[$lodgecode]['PREFIX_BOOKING_ID'];
                echo "<LI>" . $lci . "</li>"; 
            } ?>
        </ul>
    </div>

</div>

使用Javascript:

/* NG services */
var matrix = angular.module('Skimobile', ['ngTouch']);

matrix.factory('dayswidth', ['writeDays', function(){ // gets called on window change width
    return function(scope, ww) {
            var lodgename = $('.lodgename').first().width() + 4; // 4 = padding in lodgename class
            var padding = 60 + lodgename;
            var oldDisplayDays = scope.displayDays;
            scope.displayDays = Math.min(28, (ww - padding)/scope.mindaywidth); // show at most 28 days, fewer if needed to maintain minimum width
            scope.dayWidth = Math.floor( (ww-padding) / scope.displayDays );
            if(oldDisplayDays!=scope.displayDays) {   // avoid changing scope.dates as this will cause infinite recursion in the $digest on ng-repeat d in dates
                scope.callWriteDays();
            }
    };
}]);
matrix.factory('writeDays', function() {
    return function(scope) {
        var d = new Date();
        d.setTime(scope.startDay.getTime());
        scope.dates = []; // repeat on this to draw days
        scope.months = []; // repeat on this to draw months
        var yearShown = false; // only show year once, when the month is long enough
        var m = d.getMonth();
        var daysLeft = 0; // days shown belonging to each month
        for(i=0; i<scope.displayDays; i++) {
            scope.dates.push(new Date(d.getTime()));
            var oldd = new Date(d.getTime());
            d.setDate(d.getDate()+1);
            daysLeft++;
            var newm = d.getMonth();
            if(newm!=m) { // finished a month, display it
                var newMonthObj = {month:m, days:daysLeft};
                if(!yearShown && daysLeft*scope.dayWidth-1-4>3*scope.mindaywidth) {
                    newMonthObj.year = oldd.getFullYear();
                    yearShown = true;
                } else {
                    newMonthObj.year = '';
                }
                scope.months.push(newMonthObj);
                m = newm;
                daysLeft = 0;
            }
        }
        if(daysLeft>0) { // final month
            newMonthObj = {month:m, days:daysLeft};
            newMonthObj.year = yearShown ? '' : oldd.getFullYear();
            scope.months.push(newMonthObj);
        }
    }
});
matrix.factory('daystyle', function(){
    return function(scope) {
        return {
            'width': (scope.dayWidth-1) + 'px'
        }; // -1 allows for border
    }
});
matrix.factory('monthstyle', function(){
    return function(scope, m) {
        var days = m.days;
        return {
            'width': (scope.dayWidth*days-1-4) + 'px'
        } // 1 for border, 4 for padding-left
    }
});
matrix.directive('onFinishDays', function($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if(scope.$last === true) { // without this it gets called once per visible month!
                $timeout(function() {
                    scope.$emit('daysFinished');
                })
            }
        }
    }
});

/* NG controllers */
function matrixCtrl($scope, dayswidth, writeDays, daystyle, monthstyle, $swipe) {

    $scope.callDayswidth = function(w){
        dayswidth($scope, w);
    };
    $scope.callDaystyle = function() {
        return daystyle($scope);
    }
    $scope.callMonthstyle = function(m) {
        return monthstyle($scope, m);
    }
    $scope.callWriteDays = function() {
        return writeDays($scope);
    }

    $scope.data = _main; // passed via Yii layout file
    $scope.mindaywidth = 30;
    $scope.monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var d = new Date(); // initially the matrix starts from today
    $scope.startDay = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0);

    var w = $(window);
    $scope.getWindowDimensions = function() {
        return {'h': w.height(), 'w': w.width()};
    };
    $scope.$watch($scope.getWindowDimensions, function(newValue, oldValue){
        $scope.callDayswidth(newValue.w);
    }, true);
    w.bind('resize', function() {
        $scope.$apply();
    })

    $scope.showDate = function(lodge, d){
        alert(lodge.lodgetitle + ' ' + d.getDate());
    }

    $scope.swipe = {};
    $scope.nDaysFinished = 0;
    $scope.$on('daysFinished', function(event) {
        $scope.nDaysFinished++;
        $swipe.bind($('.oneday'), {
            end:function(loc){
                $scope.swipe.end = loc.x;
                $scope.swipe.daysMoved = Math.floor((loc.x - $scope.swipe.start) / $scope.dayWidth);
                $scope.startDay.setTime($scope.startDay.getTime() - $scope.swipe.daysMoved*24*60*60*1000); // compute new start day at end of drag;
                $scope.callWriteDays();
                $scope.$apply();
            }
        })
    });
}

0 个答案:

没有答案