我正在使用 Vue 和 Vuex 进行中央存储管理。 我在商店中有一个经常由 setTimeout 函数更新的对象列表。我想让用户使用双向数据绑定(bind)表单来选择和编辑它。我的问题是,每当商店中的任何数据更新时,用户正在修改的选定对象也会重新呈现。这样,用户将丢失更改。
解决方案是将对象从 Vuex 存储克隆到本地数据对象并将其绑定(bind)到表单以防止在编辑时更新。我尝试了所有可能的方法来克隆 Vuex 返回的可观察对象,但没有成功。 特别是我尝试了以下方法:
JSON.parse(JSON.stringify(obj))
和
Object.assign({}, vueObj)
以及来自外部库(如 _ 和 jQuery)的其他深度克隆方法。
这是我从 Vuex 商店获得的对象:
如果我将它字符串化,解析它并分配给本地 vue 数据对象,它会在 Vuex 中央存储更新时更新。
这是我的代码(仅组件,不是 Vuex 存储):
<template>
<div class="">
<div v-if="localSelectedDataSource.id">
{{localSelectedDataSource.name}}
</div>
<div v-if="localSelectedDataSource.id">
<div><sui-input placeholder="Url" :value="localSelectedDataSource.url"/></div>
<div>{{localSelectedDataSource.method}}</div>
<div>{{localSelectedDataSource.pollingInterval}}</div>
</div>
<div class="datasource-list">
<div
v-bind:class="{ highlightdatasource: dataSource.view.highlighted }"
v-for="dataSource in dataSources"
v-on:mouseover="highlightDataSource(dataSource.id)"
v-on:mouseout="highlightDataSource(-1)"
v-on:click="editSelectedDataSourceLocal(dataSource.id)"
>
{{dataSource.name}} - {{dataSource.url}}
</div>
</div>
</div>
</template>
<script>
import {mapGetters} from 'vuex';
import {mapActions} from 'vuex';
export default {
name: 'DataSourceList',
data(){
return{
localSelectedDataSource: {}
}
},
computed: {
...mapGetters([
'dataSources',
'selectedDataSource'
])
},
methods: {
...mapActions([
'highlightDataSource',
'editSelectedDataSource'
]),
editSelectedDataSourceLocal: function(id){
this.editSelectedDataSource(id)
var t = JSON.parse(JSON.stringify(this.selectedDataSource))
if(this.localSelectedDataSource.id != this.selectedDataSource.id){
this.localSelectedDataSource = t
}
}
}
}
</script>
谢谢
最佳答案
经过几个小时的调试,我和我的 friend 发现了我的错误:
我使用了 v-bind 简写 :
<div><sui-input placeholder="Url" :value="localSelectedDataSource.url"/></div>
而不是双向绑定(bind)v-model
<div><sui-input placeholder="Url" v-model="localSelectedDataSource.url"/></div>
所以每次中央 vuex 存储更新时,我的组件数据绑定(bind)都会重新呈现,包括绑定(bind)到表单的本地副本。
谢谢大家,
关于javascript - 用于本地编辑的 Vuex 克隆对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48292642/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser