'use strict';
/**
 * Implements directive for qusion.table component.
 */
angular.module('qusion.ui.table', [])
    .directive('qnTable', [
        function() {
            return {
                require: '?ngModel',
                link: function(scope, element, attr, controller) {

                    var settings = { key: 'ID' };
                    var getOptions = function() {
                        return angular.extend({ }, settings, scope.$eval(attr.qnTable));
                    };

                    var init = function() {
                        var options = getOptions();
                        if (controller) {
                            // Update the values when model changes
                            controller.$render = function() {
                                element.table("refresh", controller.$viewValue);
                            };
                        }

                        element.table(options);

                        if (controller) {
                            controller.$render();
                        }
                    };

                    scope.$watch(getOptions, init, true);
                }
            };
        }
    ]);