使用breeze 1.4.11和IE8时出错

时间:2014-03-31 13:37:40

标签: breeze

尝试使用breeze 1.4.11和IE8下载数据会引发以下异常:

无法解析或导入元数据:getters&无法在此javascript引擎上定义setter

错误是由b00_breeze.modelLibrary.backingStore.js的第173行引起的

创建了一个GitHub repo来重现该错误。

2 个答案:

答案 0 :(得分:2)

Breeze使用托管MVVM框架的ViewModel。这通常是一个很好的决定。此外,对实体的更改跟踪是breeze.js的基本概念(对于实体框架也是如此)。如果MVVM框架使用具有真实getter和setter的Observable(例如Knockout),则跟踪更改是一项简单的任务。另一方面,AngularJS使用纯JavaScript对象。这使得变更跟踪难度。唯一两种可靠的方法是ES5属性(简单,但IE8不支持)或$ digest循环中的深度集成。微风团队采取了首选 - 对于必须支持IE8的项目真可惜!

好的,让我们分析问题的根本原因:更改跟踪

你真的需要这个功能吗?至少在我们的项目中,我们决定使用breeze.js / OData阅读以及更多的" restful"谈到写作的方法。如果您不需要这些高级功能,则以下脚本应解决此问题:

/********************************************************
 * A replacement for the "backingStore" modelLibrary
 * 
 * This is a bare version of the original backingStore,
 * without ANY change tracking - that's why it will work in IE8!
 * (Object.defineProperty not required any more)
 *
 * This adapter is a "drop in" replacement for the "backingStore" adapter in Breeze core.
 * It has the same adapter name so it will silently replace the original "backingStore" adapter
 * when you load this script AFTER the breeze library.
 * WARNING: For obvious reasons a lot of breeze magic will be lost!
 *
 * Author: Johannes Hoppe / haushoppe-its.de
 *
 * Copyright 2014 IdeaBlade, Inc.  All Rights Reserved.  
 * Use, reproduction, distribution, and modification of this code is subject to the terms and 
 * conditions of the IdeaBlade Breeze license, available at http://www.breezejs.com/license
 ******************************************************/

(function (definition, window) {
    if (window.breeze) {
        definition(window.breeze);
    } else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
        // CommonJS or Node
        var b = require('breeze');
        definition(b);
    } else if (typeof define === "function" && define["amd"] && !window.breeze) {
        // Requirejs / AMD 
        define(['breeze'], definition);
    } else {
        throw new Error("Can't find breeze");
    }
}(function (breeze) {
    "use strict";

    var core = breeze.core;

    var ctor = function () {
        this.name = "backingStore";
        this.A_BIG_FAT_WARNING = "This is a bare version of the backingStore! Change tracking won't work!";
    };

    var protoFn = ctor.prototype;

    protoFn.initialize = function() {

    };

    protoFn.getTrackablePropertyNames = function (entity) {
        var names = [];
        for (var p in entity) {
            if (p === "entityType") continue;
            if (p === "_$typeName") continue;

            var val = entity[p];
            if (!core.isFunction(val)) {
                names.push(p);
            }
        }
        return names;
    };

    protoFn.initializeEntityPrototype = function (proto) {

        proto.getProperty = function (propertyName) {
            return this[propertyName];
        };

        proto.setProperty = function (propertyName, value) {
            this[propertyName] = value;
            return this;
        };
    };

    // This method is called when an EntityAspect is first created - this will occur as part of the entityType.createEntity call.
    // which can be called either directly or via standard query materialization
    // entity is either an entity or a complexObject
    protoFn.startTracking = function (entity, proto) {

        // assign default values to the entity
        var stype = entity.entityType || entity.complexType;
        stype.getProperties().forEach(function (prop) {

            var propName = prop.name;
            var val = entity[propName];

            if (prop.isDataProperty) {
                if (prop.isComplexProperty) {
                    if (prop.isScalar) {
                        val = prop.dataType._createInstanceCore(entity, prop);
                    } else {
                        val = breeze.makeComplexArray([], entity, prop);
                    }
                } else if (!prop.isScalar) {
                    val = breeze.makePrimitiveArray([], entity, prop);
                } else if (val === undefined) {
                    val = prop.defaultValue;
                }

            } else if (prop.isNavigationProperty) {
                if (val !== undefined) {
                    throw new Error("Cannot assign a navigation property in an entity ctor.: " + prop.Name);
                }
                if (prop.isScalar) {
                    // TODO: change this to nullstob later.
                    val = null;
                } else {
                    val = breeze.makeRelationArray([], entity, prop);
                }
            } else {
                throw new Error("unknown property: " + propName);
            }
            entity[propName] = val;
        });
    };

    breeze.config.registerAdapter("modelLibrary", ctor);
}, this));

下载地址:https://gist.github.com/JohannesHoppe/72d7916aeb08897bd256

这是原始backingStore的裸版,没有任何更改跟踪 - 这就是它在IE8中工作的原因! (不再需要Object.defineProperty)这个适配器是"放入"替换" backingStore"适配器在Breeze核心。它具有相同的适配器名称,因此它将静默替换原始" backingStore"在微风库之后加载脚本时适配器。

这是一个证明功能的演示:
http://jsfiddle.net/Johannes_Hoppe/bcav9hzL/5/

JsFiddle不支持IE8,请使用此直接链接:
http://jsfiddle.net/Johannes_Hoppe/bcav9hzL/5/embedded/result/

干杯!

答案 1 :(得分:0)

IE8中明确不支持Breeze'backupStore'模型库。这是因为'backingStore'实现需要完整的javascript'Object.defineProperty'功能,这在IE8中是不可用的,并且不能由任何垫片提供。

也就是说,微风'淘汰'和'骨干'模型库适配器都应该与IE8一起使用。

另见:http://www.breezejs.com/documentation/prerequisites