Beispiel #1
0
    setOptions: function (data, modelParams) {
        var self = this;
        var optionsPromise = new can.Deferred();
        var ajaxOptions = false;

        var makeOptionsArray = function (object) {
            object = object || [];
            if (!can.isArray(object)) {
                var newData = [];
                for (var i in object) {
                    if (object.hasOwnProperty(i)) {
                        newData.push({
                            value: i,
                            text: object[i]
                        });
                    }
                }
                return newData;
            } else {
                return object;
            }
        };

        if (can.isDeferred(data)) {
            optionsPromise = data.then(function (options) {
                options = makeOptionsArray(options);
                self.$input.on('shown.bs.select', function () {
                    if (typeof self._parameters.shownCallback === 'function') {
                        self._parameters.shownCallback();
                    }
                });

                return options.concat(self._getStaticOptions());
            });
        } else if (data && typeof data.findAll === 'function') {
            ajaxOptions = true;
            this._setAjaxOptions(data, modelParams);
            optionsPromise.resolve(this._getStaticOptions());
        } else {
            data = makeOptionsArray(data);
            var optionsData = data.concat(this._getStaticOptions());
            self.$input.on('shown.bs.select', function () {
                if (typeof self._parameters.shownCallback === 'function') {
                    self._parameters.shownCallback();
                }
            });

            optionsPromise.resolve(optionsData);
        }

        optionsPromise.then(function (options) {
            if (!ajaxOptions && typeof self.value() === 'undefined' && options.length > 0) {
                self.value(options[0].value);
            }
            self.options.replace(self._removeDuplicateOptions(options));
        });
    },
Beispiel #2
0
    _prefetchValue: function (value) {
        var self = this;
        var deferred = new can.Deferred();

        value = this._makeArray(value);

        var values = [];
        var valuesToFetch = [];

        value.forEach(function (val) {
            if (typeof self.optionsCache[val] !== 'undefined') {
                values.push({
                    value: val,
                    text: self.optionsCache[val]
                });
            } else {
                valuesToFetch.push(val);
            }
        });

        if (this.ajaxInfo && valuesToFetch.length > 0) {

            var prefetchData = {};

            var reservedKeys = ['search'].concat(this.reservedKeys.serialize());

            var ajaxData = this.ajaxInfo.params.attr('');

            for (var prop in ajaxData) {
                if (ajaxData.hasOwnProperty(prop) && reservedKeys.indexOf(prop) === -1) {
                    prefetchData[prop] = ajaxData[prop];
                }
            }

            prefetchData.limit = valuesToFetch.length;
            prefetchData[this.ajaxInfo.params.dataValueField] = valuesToFetch.join(',');

            if (typeof prefetchData.fields !== 'string') {
                prefetchData.fields = prefetchData.fields.join(',');
            }

            can.ajax({
                url: this.ajaxInfo.model.resource,
                type: 'GET',
                data: prefetchData
            }).then(function (data) {
                self._replaceOptionsFromRequest(data);
                deferred.resolve();
            });
        } else {
            this.options.replace(this._removeDuplicateOptions(values));
            deferred.resolve();
        }

        return deferred;
    },
Beispiel #3
0
    showForm: function (entityToEdit, $rowElement) {
        var self = this;
        var deferred = new can.Deferred();

        var setData = function (entity) {
            var formType = 'insert';
            if (entity) {
                formType = 'update';
                self.$rowElement = $rowElement;
            } else {
                entity = new self.configuration.resource();
            }

            self.formConfig.setData(entity);
            self.formConfig.setDefaultValues();
            self.attr('visibleForm', formType);
            self.formConfig.initFields();
            self.formEntity.createSnapshot();
        };

        if (this.formConfig) {

            if (entityToEdit === this.formConfig.entity) {
                this.hideForm();
                deferred.resolve(false);
            } else if (this.formEntity && this.formEntity.isModified()) {
                this.showPrompt('Really ?')
                    .then(function(yes) {
                        if (yes) {
                            self.formEntity.revertSnapshot();
                            setData(entityToEdit);
                            self.animateForm(deferred);
                        } else {
                            deferred.resolve(false);
                        }
                    });
            } else {
                setData(entityToEdit);
                self.animateForm(deferred);
            }
        } else {
            deferred.resolve(false);
        }

        return deferred.promise();
    },
Beispiel #4
0
			get: function(list){
				var id = "foo";
				var dfd = new can.Deferred();
				canWait.data(dfd.then(function(value){
					return {
						pageData: {
							restaurant: {
								"{\"foo\":\"foo\"}": value
							}
						}
					}
				}));

				list.replace(dfd);
				dfd.resolve([ { a: "a" }, { b: "b" } ]);

				return list;
			}
Beispiel #5
0
 .then(function(yes) {
     if (yes) {
         self.formEntity.revertSnapshot();
         setData(entityToEdit);
         self.animateForm(deferred);
     } else {
         deferred.resolve(false);
     }
 });
Beispiel #6
0
 }).then(function (data) {
     self._replaceOptionsFromRequest(data);
     deferred.resolve();
 });