loopback model with object

好久不见. 提交于 2019-12-12 19:06:07

问题


Good evening,

I just started with loopback (literally), so I went for the commands explained in the documentation:

Started the loopback project with the command:

slc loopback

Generated a model with the loopback model generator with the command:

slc loopback:model

When the generator starts, it asks for model name, storing method, base class, plural, whether we want it to be a common model or a server model. After that it asks for the properties of the model.

I have a model that could be like this:

MODEL NAME
Property1 : String,
Property2 : String,
Property3 : Number,
Property4 : {
            obj.Property1 : String,
            obj.Property2 : String,
            obj.Property3 : String
            },
Property5 : String

I thought that by choosing "Object" as the type of property it would ask me for additional properties on that object, but it didn't. Now I have no idea how to create those additional properties that are inside the object of this model.

How would I go about creating the properties that are nested inside the Property4 Object? Am I missing something from the loopback:model generator?


回答1:


Well slc loopback:model doesn't do that. You just have to specify yourself in the generated json file(possibly in common/models/ directory) in the properties object:

"properties": {
  ...
  "Property4": {
    "type": {
      "Property1" : "String",
      "Property2" : "String",
      "Property3" : "String"
    }
  },
  ...
}

If you want any of the properties to be required you can do it like this:

"properties": {
  ...
  "Property4": {
    "type": {
      "Property1" : {
        "type": "String"
        "required": true
      },
      "Property2" : "String",
      "Property3" : "String"
    }
  },
  ...
}

If the object doesn't have a "type" property, you can just do this:

"properties": {
  ...
  "Property4": {
    "Property1" : "String",
    "Property2" : "String",
    "Property3" : "String"
  },
  ...
}

You can also put this definition into a another model and reference that here:

"properties": {
  ...
  "Property4": "AnotherModel",
  ...
}

I suggest you read through the 'properties' section of this document and also 'object types' section of this document.



来源:https://stackoverflow.com/questions/42137597/loopback-model-with-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!