Addison

6219 经验值

<body>
<div class="container">
    <form class="add-comment">
        <textarea class="comment-text"></textarea><br>
        <button type="button" class="btn btn-primary" value="submit">Post Comment</button>
    </form>
    <div class="comment"></div>
</div>
<script>
    const addCommentForm = document.querySelector('.add-comment');
    const textarea = document.querySelector('.comment-text');
    const commentDiv = document.querySelector('.comment');
    const user = 'Mary';

    addCommentForm.addEventListener('submit', function (event) {
        event.preventDefault();
        const newConmment = textarea.value.trim();
        if (newConmment)
        {
            commentDiv.innerHTML = `
            <div class=" comment-header">${user}</div>
            <div class="comment-body">${textarea.value}</div>
            `
            textarea.value = ``;
        }
    })
</script>
</body>

文本框内容不能成功提交。。。

我的还是实现不了哦
尴尬C:\Users\janicerant\Desktop\webpack-learning>webpack
C:\Users\janicerant\Desktop\webpack-learning\webpack.config.
js:23

},
 ^

SyntaxError: Invalid or unexpected token

at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at requireConfig (C:\Users\janicerant\AppData\Roaming\np

m\node_modules\webpack\bin\convert-argv.js:97:18)

{

            test:/\.js$/,
            loader:"babel-loader",
            exclude:/node_modules/,
        }
    ]
},
babel:{
    preset: ['es2015','stage-0'],
    plugins: ['trsnsform-runtime']

}
};

箭头函数的特点第三个是什么呀

比如有一个 Model 类名称为UserPost,那么其数据表默认蛇形名称为: user_posts.在程序语言中,蛇形名称也常用于变量/方法的命名,与驼峰相对应.

@JellyBool, 谢谢。我从How to use Transformer in one to many relationship. #1054找到了更好的解决方案。
先上代码:

DepartmentTransformer

class DepartmentTransformer extends TransformerAbstract
{
    public function transform($department)
    {
        return [
            'id' => $department['id'],
            'name' => $department['name'],
            'level' => $department['level'],
            'parent_id' => $department['parent_id']
        ];
    }
}

RolesTransformer

class RolesTransformer extends TransformerAbstract
{
    public function transform($role)
    {
        return [
            'name' => $role['name'],
            'slug' => $role['slug'],
            'description' => $role['description'],
            'level' => $role['level']
        ];
    }

}

UserTransformer

class UserTransformer extends TransformerAbstract
{
    protected $defaultIncludes = ['departments','roles'];


    public function transform($user)
    {
        return [
            'id' => $user['id'],
            'name' => $user['name'],
            'email' => $user['email'],
            'phone' => $user['phone'],
        ];
    }


    public function includeDepartments(User $user)
    {
        $dept  = $user->departments;

        return $this->collection($dept, new DepartmentTransformer());
    }


    public function includeRoles(User $user)
    {
        $rl = $user->roles;

        return $this->collection($rl, new RolesTransformer());
    }
}

然后,我在 Controller 里面调用

$user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();

return $this->response->collection($user, new UserTransformer());

得到如下返回

           "data": {
            {
                "id": 43,
              "name": "test7",
              "email": "test7@foxmail.com",
              "phone": "18679152257",
              "departments": {
                "data": {
                  {
                      "id": 1,
                    "name": "业务一部",
                    "level": 1,
                    "parent_id": 0
                  }
                }
              },
              "roles": {
                "data": {
                  {
                      "name": "agent",
                    "slug": "agent",
                    "description": "业务员",
                    "level": 1
                  }
                }
              }
            }
          }

@JellyBool,本人在使用 dingo/api 组件时,不知道如何返回数据表的关联结果。特来请教。
先上代码。

    /**
     * 创建一个新用户,同时设置角色与所属部门
     * @Post("/")
     * @Versions({"v1"})
     * @Transaction({
     *      @Request({"name": "admin", "email": "admin@jiuxiao.com","phone":"18679152251","role_id":"2",
     *     "department_id":"1"},
     * headers={"Authorization": "Bearer  token_value"}),
     *      @Response(200, body={"data": {"id": 10, "username": "admin" ),
     *      @Response(500, body={"message": "xxx","code":"23000"})
     * })
     * @Parameters ({
     *      @Parameter("name", description="用户名",required=true),
     *      @Parameter("phone", description="用户手机.",required=true),
     *      @Parameter("email", description="用户邮箱.",required=true),
     *      @Parameter("role_id", description="角色 id.",required=true),
     *      @Parameter("department_id", description="部门 id.",required=true),
     * })
     * @param AddUserRequest $request 表单提交请求
     * @return \Dingo\Api\Http\Response|void
     */
    public function store(AddUserRequest $request)
    {
        $input = [
            'email' => $request->get('email'),
            'name' => $request->get('name'),
            'password' => password_hash(self::DEFAULT_PASSWORD, PASSWORD_DEFAULT),
            'role_id' => $request->get('role_id'),
            'department_id' => $request->get('department_id'),
            'phone' => $request->get('phone'),
        ];

        try{
            DB::transaction(function () use ($input){
                $user = User::create($input);

                $role = Role::findOrFail($input['role_id']);
                $user->attachRole($role);

                $department = Department::findOrFail($input['department_id']);
                $user->departments()->save($department);
            });

            $user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();

            return $this->response->item($user, new UserTransformer());

        }catch (Exception $e){
            return $this->response->errorInternal('用户创建失败');
        }

    }

其中,UserTransformer 代码如下:

class UserTransformer extends TransformerAbstract
{
    public function transform($user)
    {
        return [
            'id' => $user['id'],
            'name' => $user['name'],
            'email' => $user['email']
        ];
    }
}

在 UsersController 里的结果返回,我自己觉得使用如下代码是不对的。因为查询到的结果是3个表的关联查询后的结合。如何使用 Transformer 处理 这种情况。

return $this->response->item($user, new UserTransformer());

从 Illuminate\Database\Eloquent\Model 抽象基类的代码来理解可能会有帮助

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [];

Eloquent 模型约定
除非数据表明确地指定了其它名称,否则将使用类的「蛇形名称」、复数形式名称来作为数据表的名称