ExoWeb.Templates.JavaScript.Marshaler.Wrap C# (CSharp) Method

Wrap() public method

public Wrap ( ModelInstance instance ) : object
instance ModelInstance
return object
        public object Wrap(ModelInstance instance)
        {
            if (instance == null)
                return Null.Value;

            string key = instance.Type.Name + "|" + instance.Id;

            EntityWrapper entity;

            if (entities.TryGetValue(key, out entity))
                return entity;

            entity = new EntityWrapper(engine, instance, this);
            entities.Add(key, entity);

            return entity;
        }

Same methods

Marshaler::Wrap ( object clrObject ) : object

Usage Example

Example #1
0
        protected override object GetMissingPropertyValue(string jsPropertyName)
        {
            // special meta property
            if (jsPropertyName == "meta")
            {
                return(LazyDefineProperty("meta", new Meta(Engine, RealObject)));
            }

            // handle model properties
            string modelPropertyName;

            if (jsPropertyName.StartsWith(GetterPrefix))
            {
                modelPropertyName = jsPropertyName.Substring(GetterPrefix.Length);
            }
            else if (jsPropertyName.StartsWith(SetterPrefix))
            {
                throw new InvalidOperationException("Properties are read-only");
            }
            else
            {
                throw new InvalidOperationException("Only property get accessors are supported on model objects: " + jsPropertyName);
            }

            ModelProperty property = RealObject.Type.Properties[modelPropertyName];

            if (property == null)
            {
                throw new InvalidPropertyException(RealObject.Type, modelPropertyName);
            }

            if (property is ModelValueProperty)
            {
                // optimization: cast outside of delegate
                ModelValueProperty valueProperty = (ModelValueProperty)property;

                return(LazyDefineMethod(jsPropertyName, instance =>
                {
                    var value = instance.GetValue(valueProperty);

                    if (value is decimal)
                    {
                        return decimal.ToDouble((decimal)value);
                    }

                    if (value is float)
                    {
                        return Convert.ToDouble((float)value);
                    }

                    if (value is long)
                    {
                        return Convert.ToInt32((long)value);
                    }

                    if (value is DateTime)
                    {
                        var dateTime = (DateTime)value;

                        // Use the same serialization format as `JsonUtility.UtcDateTimeConverter`.
                        var dateString = dateTime.ToUniversalTime().ToString(@"yyyy-MM-dd\THH:mm:ss.fff\Z");

                        var jsDate = Engine.Date.Construct(dateString);

                        return jsDate;
                    }

                    return value;
                }));
            }

            ModelReferenceProperty refProperty = (ModelReferenceProperty)property;

            if (refProperty.IsList)
            {
                return(LazyDefineMethod(jsPropertyName, instance =>
                {
                    return factory.Wrap(instance.GetList(refProperty));
                }));
            }
            else
            {
                return(LazyDefineMethod(jsPropertyName, instance =>
                {
                    return factory.Wrap(instance.GetReference(refProperty));
                }));
            }
        }
All Usage Examples Of ExoWeb.Templates.JavaScript.Marshaler::Wrap