Example usage for com.google.gwt.query.client.js JsMap create

List of usage examples for com.google.gwt.query.client.js JsMap create

Introduction

In this page you can find the example usage for com.google.gwt.query.client.js JsMap create.

Prototype

public static <S, T> JsMap<S, T> create() 

Source Link

Usage

From source file:org.lirazs.gbackbone.client.core.collection.Collection.java

License:Apache License

public Collection set(Options[] objects, Options options) {
    if (objects == null)
        return this;

    options = new Options().defaults(options, setOptions);

    Integer at = options.getInt("at");
    if (at != null) {
        at = +at;/*from w w  w  .j  a va2 s  . c  o  m*/
        if (at < 0)
            at += length + 1;
        if (at < 0)
            at = 0;
    }

    boolean sort = false;
    boolean sortable = hasComparator() && !options.containsKey("at")
            && (!options.containsKey("sort") || options.getBoolean("sort"));

    List<T> toAdd = new ArrayList<T>();
    List<T> toRemove = new ArrayList<T>();
    Set<T> toOrder = new LinkedHashSet<T>();
    JsMap<String, Boolean> modelMap = JsMap.create();

    boolean add = options.getBoolean("add");
    boolean merge = options.getBoolean("merge");
    boolean remove = options.getBoolean("remove");

    boolean order = !sortable && add && remove;

    // Turn bare objects into model references, and prevent invalid models
    // from being added.
    for (Options model : objects) {
        T preparedModel = prepareModel(model);

        //if(model == null)
        //    model = preparedModel;

        //if(preparedModel != null) {
        if (model != null) {
            T existing = get(preparedModel);
            // If a duplicate is found, prevent it from being added and
            // optionally merge it into the existing model.
            if (existing != null) {
                if (remove)
                    modelMap.put(existing.getCid(), true);

                if (merge) {
                    //Options attrs = (preparedModel == model) ? model.getAttributes() : options.<Options>get("_attrs");
                    Options attrs = model;
                    existing.set(attrs, options);

                    if (sortable && !options.getBoolean("sort"))
                        sort = true;
                }
            } else if (add) { // This is a new model, push it to the `toAdd` list
                toAdd.add(preparedModel);

                // Listen to added models' events, and index models for lookup by
                // `id` and by `cid`.
                preparedModel.on("all", onModelEvent, this);
                byId.put(preparedModel.getCid(), preparedModel);

                if (!preparedModel.isNew())
                    byId.put(preparedModel.getId(), preparedModel);
            }
            if (order) {
                toOrder.add(existing != null ? existing : preparedModel);
            }

            options.remove("_attrs");
        }
    }

    // Remove nonexistent models if appropriate.
    if (remove) {
        for (int i = 0; i < length; i++) {
            T model = this.models.get(i);
            if (modelMap.get(model.getCid()) == null)
                toRemove.add(model);
        }

        if (toRemove.size() > 0)
            removeModels(toRemove, options);
    }

    boolean orderChanged = false;
    if (toAdd.size() > 0 || (order && toOrder.size() > 0)) {
        if (sortable)
            sort = true;

        length += toAdd.size();

        orderChanged = length() != toOrder.size() || toAdd.size() > 0;
        if (!orderChanged) {
            Iterator<T> iterator = toOrder.iterator();

            for (T model : this.models) {
                if (model != iterator.next()) {
                    orderChanged = true;
                    break;
                }
            }
        }

        if (options.containsKey("at")) {
            this.models.addAll(at, toAdd);
        } else {
            if (order)
                this.models.clear();

            //Array.prototype.push.apply(this.models, order || toAdd);
            this.models.addAll((order && toOrder.size() > 0) ? toOrder : toAdd);
        }
    }

    if (sort)
        this.sort(new Options("silent", true));

    // Unless silenced, it's time to fire all appropriate add/sort events.
    if (!options.getBoolean("silent")) {
        for (int i = 0; i < toAdd.size(); i++) {
            if (options.containsKey("at"))
                options.put("index", at + i);

            T model = toAdd.get(i);
            model.trigger("add", model, this, options);
        }

        if (sort || (orderChanged && order && toOrder.size() > 0)) {
            this.trigger("sort", this, options);
        }
        if (toAdd.size() > 0 || toRemove.size() > 0) {
            this.trigger("update", this, options);
        }
    }

    return this;
}

From source file:org.lirazs.gbackbone.client.core.collection.Collection.java

License:Apache License

public Collection set(List<T> models, Options options) {
    if (models == null)
        return this;

    options = new Options().defaults(options, setOptions);

    Integer at = options.getInt("at");
    if (at != null) {
        at = +at;/* w w  w  .ja va2s .  com*/
        if (at < 0)
            at += length + 1;
        if (at < 0)
            at = 0;
    }

    boolean sort = false;
    boolean sortable = hasComparator() && !options.containsKey("at")
            && (!options.containsKey("sort") || options.getBoolean("sort"));

    List<T> toAdd = new ArrayList<T>();
    List<T> toRemove = new ArrayList<T>();
    Set<T> toOrder = new LinkedHashSet<T>();
    JsMap<String, Boolean> modelMap = JsMap.create();

    boolean add = options.getBoolean("add");
    boolean merge = options.getBoolean("merge");
    boolean remove = options.getBoolean("remove");

    boolean order = !sortable && add && remove;

    // Turn bare objects into model references, and prevent invalid models
    // from being added.
    for (int i = 0; i < models.size(); i++) {
        T model = models.get(i);
        T preparedModel = prepareModel(model);

        if (model == null)
            model = preparedModel;

        if (preparedModel != null) {
            T existing = get(preparedModel);
            // If a duplicate is found, prevent it from being added and
            // optionally merge it into the existing model.
            if (existing != null) {
                if (remove)
                    modelMap.put(existing.getCid(), true);

                if (merge) {
                    Options attrs = (preparedModel == model) ? model.getAttributes()
                            : options.<Options>get("_attrs");
                    existing.set(attrs, options);

                    if (sortable && !options.getBoolean("sort") && existing.hasChanged())
                        sort = true;
                }
            } else if (add) { // This is a new model, push it to the `toAdd` list
                toAdd.add(model);

                // Listen to added models' events, and index models for lookup by
                // `id` and by `cid`.
                model.on("all", onModelEvent, this);
                byId.put(model.getCid(), model);

                if (!model.isNew())
                    byId.put(model.getId(), model);
            }
            if (order) {
                toOrder.add(existing != null ? existing : model);
            }

            options.remove("_attrs");
        }
    }

    // Remove nonexistent models if appropriate.
    if (remove) {
        for (int i = 0; i < length; i++) {
            T model = this.models.get(i);
            if (modelMap.get(model.getCid()) == null)
                toRemove.add(model);
        }

        if (toRemove.size() > 0)
            remove(toRemove, options);
    }

    boolean orderChanged = false;
    if (toAdd.size() > 0 || (order && toOrder.size() > 0)) {
        if (sortable)
            sort = true;

        length += toAdd.size();

        orderChanged = length() != toOrder.size() || toAdd.size() > 0;
        if (!orderChanged) {
            Iterator<T> iterator = toOrder.iterator();

            for (T model : this.models) {
                if (model != iterator.next()) {
                    orderChanged = true;
                    break;
                }
            }
        }

        if (options.containsKey("at")) {
            this.models.addAll(at, toAdd);
        } else {
            if (order)
                this.models.clear();

            this.models.addAll((order && toOrder.size() > 0) ? toOrder : toAdd);
        }
    }

    // Silently sort the collection if appropriate.
    if (sort)
        this.sort(new Options("silent", true));

    // Unless silenced, it's time to fire all appropriate add/sort events.
    if (!options.getBoolean("silent")) {
        for (int i = 0; i < toAdd.size(); i++) {
            if (options.containsKey("at"))
                options.put("index", at + i);

            T model = toAdd.get(i);
            model.trigger("add", model, this, options);
        }

        if (sort || (orderChanged && order && toOrder.size() > 0)) {
            this.trigger("sort", this, options);
        }
        if (toAdd.size() > 0 || toRemove.size() > 0) {
            this.trigger("update", this, options);
        }
    }

    return this;
}

From source file:org.lirazs.gbackbone.client.core.net.NetworkSyncStrategy.java

License:Apache License

protected NetworkSyncStrategy() {
    methodMap = JsMap.create();
    methodMap.put("create", "POST");
    methodMap.put("update", "PUT");
    methodMap.put("patch", "PATCH");
    methodMap.put("delete", "DELETE");
    methodMap.put("read", "GET");
}